Steps to provide customized validation messages

Steps to provide customized validation messages

Step1 – Prepare a properties file with the custom validation msgs

There prepare a properties file under classes folder or under src folder, pass all the predefined validation msgs in the present properties file from messages. Properties file and edit the validation message as per our requirement

Eg: abc. Properties

 

Should not be less than{0}

Javax. faces. Validator. Long range Validator. maximum={1}value should not be greater than{0}

 

Step – 2

Configure properties file in faces-config.xml file

To configure validation messages properties file, we have to use the following tags in faces- config.xml file

<faces-config>

<application>

<message-bundle>abc</message-bundle>

</application>

</faces-config>

 

Step – 3

Provide label attribute in the respective i/p components

The main intension of label attribute is to override the default component id-component name in validation messages with the provided value

<h:input text id=”uname” value=”#{reg Bean. Uname}” required=”true” label=”user name”/>

 

Screenshot_19

 

 

abc. properties

Javar. faces. Validator. NOT-IN-RANGE=value is not in the range from{0}to{1}.

Javax. faces. Validator. Double range Validator. NOT-IN-RANGE= {2}.value must be in between the range {0} and{1}

Javax. faces. validator. Double range validator. TYPE={0} must be Double

Javax. faces. validator. Long range validator. NOT-IN RANGE ={2} value must be in the  range {0} and{1}

Javax. faces. validator. Long range validator. TYPE={0} must be Double long

Javax. faces. Component . UIInput. REQUIRED={0} Is required

 

Index.html

<html>

<head>

<meta http-equiy=”Refresh”

Content=”2:URL=http://local host:1010/standaral dationsapp/registration from.dss”>

<title>insert title here </title>

</ head >

</f:view>

<body>

<h1>application Loading….< /h1>

</body>

</html> 

 

Registration form.jsp:

<%@page language=”java”conten type=”text/html;”>

<%@taglib prefix=”f”

<!Doctype—

<html>

<head >

<meta http-equiv=” Content type” Content= ”text/ html; chanset= ISO-8859-1”>

<TITLE>Insert title here</title>

</head >

<body>

<f:view>

<h:from>

<h:messages style=”color: blue”/>

<h:panel Grid columns=”3”>

<h: input text id=”u name” value=”#{reg bean. uname}” required=”true” label=”user name”/>

<h: messages for= ”uname” style=”color:red”/>

<h:output text value=”password”/>

<h:input secret id=”upwd”value=”#{ reg bean. upwd}”

required=”true” label=”password”/>

<f:validate length minimum=”6”maximum=”15”/>

</h:input secret>

<h: messages for= ”upwd” style=”color: red”/>

<h: output text value=”user age”/>

<h:input text id=”uaage” value=”#{ reg bean. uage}”

required=”true” label=”user age”>

<f:validate long range minimum=”18”maximum=”30”/>

<h:input text>

<h: messages for= ”uage” style=”color: red”/>

<h: output text value=”user salary”/>

<h:input text id=”usal” value=”#{ reg bean. usal}”

required=”true” label=”user salary”>

<f:validate Double range minimum=”25000”maximum=”50000”/>

<h:input text>

<h: messages for= ”usal” style=”color: red”/>

<h:command button value=”Registration”action=”#

{reg bean. registration”} ”/>

</h:panel Grid>

</h:form></f:view>

</body></html>

 

Success.JSP

<head >

<meta……”>

<title>insert title here<1title>

</head>

<body>

<f:view>

<h1>Registration success</h1>

</f:view>

</body></html>

 

Registration Bean.java

Package com. soft;

Public class Registration Bean{

Private string uname;

Private string upwd;

Private int uage;

Private floal usal;

Set…()

Get…()

Public string registration(){

Return ”success”;

}

}

 

Faces-config.xml

<?xml version=”1.0”encoding=”UTF-8”?>

<FACES-Config

Xmlns=—-xsd ”version=”1.2”>

<managed-bean>

<managed-bean-name>reg bean</managed-bean-name >

<managed-bean-class>com. Durga soft. registration B ean</managed-bean- class >

<managed-bean-scope>session</managed-bean- scope > </managed-bean >

<navigation-rule>

<from-view-id>/registration from.jsp</  >

<navigation-case>

<from-out come>success</  >

<to-view- id>/success.jsp</  >

</navigation-case>

</navigation-rule>

<application>

<message-bundle>abc</   >

</application>

</faces-config> 

 

Web.xml same as prev .appl.?

Custom validations: In JSF based web application , if all the standard validations are not sufficient to perform data validations, then JSF has given an environment to prepare user defined validations

  • To prepare custom Validator, we have to use the following steps
  1. Prepare user defined Validator class.
  2. Configure custom Validator in faces-config.xml file
  • Use custom valid tore in faces-config.xml component in user forms,

Prepare user defined Validator class. for this we have to take an user defined class & it must implement javax.faces. Validator. Validator interface

  • Validator interface has the following method to provide custom validation logic

Public void validate(faces context fc, UI Component UC Object value)throws Validator Exception;

  • In validate()method we have to provide the following implementation.

 

  1. a) prepare faces message object to represent a single validation message faces message fm=new faces message();
  2. b) Add validation message to faces message object by using either of the following methods

Public void set detail(string msg)

Public void set summary(string msg)

Msg. set detail(“Invalid mobile number”);

Msg. set summary(“Invalid mobile number”);

  1. c) Rise Validator Exception with the faces message throw new Validator exception(fon)

 

ii) Configure custom Validator in faces-config.xml files:

To configure custom Validator in faces-config.xml file we have to use the following tags

<faces-config>

<Validator>

<Validator-id>logical-name</

<Validator-class>custom Validator fully qualified name</

<Validator>

</faces-config>

 

iii) Use custom validator to the required i/p component

to use custom validator with i/p component have to use the following tag

<f:validator validator Id=-validator Id=….”/>

This tag must be used under<h:input text>iag

 

Custom validator app(1.x custom validator)

ABC.properties

Eid=Employee id

ename =Employee name

eaddres =Employee addres

eemail =Employee mail ID

eemobile =Employee mobile

button=Registration

email-validatore=invalid email id

//copy standard Validator msg from messages. Properties file and modify them

 

Index.html

<html>

<head>

<meta http-equiv=”Refresh”content=”2;url=http://local host:100/custom validate app/employee form.dss”>

<body>

<h1>application is loading</h1>

</body>

</html>

 

Employee form.jsp

%@laglib perfix=”f”url=”http//sun.java.com/jsf/care%/html%

<%

<html>

<body>

<f:view>

<font color=”red”>

<h1> software solutions</h1>

<h2>employee registration form</h2>

</font>

<h:form>

<h:panel grid=columns=”3”>

<h:output text value=”#{msg.eid}”/>

<h:input text id=”eid” value=”#{regBean.eid}”required

=”true ”label=”Employee Id”/>

<h:message for=”eid”/>

<h:output text value=”#{msg. ename}”/>

<h:input text id=”ename” value=”#{regBean. ename}”

Required=”true”label=”employee name”/>

<h:message for=”ename”/>

<h:output text value=”#{msg. eaddr}”/>

<h:input text id=”eaddre” value=”#{reg Bean. eaddr}”

Required=”true” label=”employee address”/>

<h:message for=”eaddr”/>

<h: output text value=”#{msg. eemail}”/>

<h:input text id=”eemail” value=”#{reg Bean. eemail}”

Required=”true” label=”employee email”/>

<f:validator validator id=”email validator”/>

</h:input text>

<h:message for=”eemail’/>

<h: output text value=”#{msg. emobile}”/>

<h:input text id=”emobile” value=”#{reg Bean. eemail}”

Required=”true” label=”employee mobile”/>

<h:message for=”emobile”/>

<h:command button value=”#{reg button}”action=”#{

Reg bean. registration}”/>

</h:panel Grid></h:form></f:view>

</body></html>

 

Success.jsp

<%@laglib prefir=”f”—–core”%>

<                            “h”                     html”%>

<html>

<body>

<f:view>

<h1>Registration success</h1>

</f:view>

</body>

</html>

 

Registration Bean.java

Package com. soft

Public class Registration Bean{

Private string eid;

Private string ename;

Private string eaddr;

Private string eemail;

Private string emobile;

//provide setter()&getter()getter()

Private string registration(){

Return ”success”;

} 

 

Email validator.java

Package com. Durga soft;

Import java. util. iterator;

Import java. util. Resource bundle;

Import java. faces. application. application;

Import java. Faces. application. faces message;

Import java. faces. content. faces context;

Import java. faces. Validator. validator;

Import java. faces. validator. validator exception;

Public class email validator implements validator{

Public void validator(faces context fe,UI Component we, object value) throws validator exception{

String email.=(string)value;

If(email. contains(“@”)){

Application application=fe.get application();

String prp-file=application. get message bundle();

Resource bundle bundle=Resource bundle. get bundle(prp-file);

String err-msg=bundle.get string(“email-validator”);

Faces message fm=new faces message();

Fm.set detail(err-msg);

Throw new validator Exception (fm);

}

}

}

 

Faces-config.xml

<faces-config>

——-

—— XSD

<Managed. bean>

< Managed. Bean-name>reg bean</ Managed. Bean-name>

< Managed. Bean-class>com. durga soft. Registration bean reg

</    >

Managed. Bean-scope>session </    >

< /Managed .bean>

<navigation-rule>

<from-view-id>/emkployeeform.jsp</    >

<navigation-case>

<from-outcome>success</    >

<to-view-id>/ success.jsp</    >

</navigation-rule>

<validator>

<validator-id>email-validator</    >

</validator>

<application>

<resource-bundle>

<base-name>abc</b >

<var>msg<va/r>

<message-bundle>abc< /message-bundle >

</application>

</faces-config>

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Steps to provide customized validation messages"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please leave a message and we'll get back to you soon.
    Three + 6