VALIDATIONS in JSF

Ratings:
(4)
Views: 187
Banner-Img
Share this blog:

In  general, in JSF based web applications, it we submit a form data to server then container will take from data request parameters data and store in request object, where faces context will take from data from request object and store in component tree as part at “Apply request values” life cycle phase. After getting form data in component tree, faces servlet will store form data from component tree to model object as part of “update model” life cycle phase after getting data in managed bean object, we will use form data in application logic as part of “Invoke application” life cycle phase.

  • In the above context, it the form data provided by user is proper then we will not get any exception while executing application, it the form data provided by user is improper then our application may not be executed and there may be a chance to get exceptions.
  • To over come the above problem, we have to check weather data is valid or not before using data in application logic
  • To check data validity before using data in application logic, we have to go for ”data validations”
  • In web applications, there are two places to check data validations
  1. At client side
  2. At server side

 

Client side data validations

If we check data validity just before submitting form to server then it is called as client side data validations.

  • To perform client side data validations, we have to execute ”java script” functions either directly are indirectly at client browser.
  1. Client side data validations are not suggestible in web application, because, then data validations ”java.script” function at client browser then data validations will not be executed.
  2. At client side data validations, data will be interpreted at the same client machine, so that, client side data validations will provide less security for the data

To overcome the above problems, we have to go for server side data validations  

  1. Server side data validations

It we check data validity after strong data in component tree and just before passing data to managed bean object then it is called as server side data validations.

  • To perform data validations at server side, we will use java code directly it is not required to use java script fun
  • To perform server side data validations,JSF-1.2 Version has provided the following two types of data validations
  1. Standard validations
  2. Custom validations

 

  1. Standard validations

Standard data validations are the data validations provided by ISF frame work predefinetly

  • 2 Version has provided the following for types at data validations
  1. Required
  2. Length validations
  3. Long range validations
  4. Double Range validations

  Required It is an attribute at each and every input component to check weather data is available or not

  • It data is not available then this attribute will display validation message

  NOTE In JSF all the predefined Validator are having their own predefined message provided by JSF at the following location Extract JSF-api.jaràJavaxàfacesàmessage.properties

  • For the required validation JSF has provided predefined validation message in the form of the following key-value pair at message properties file in ‘component errors’.

Javax. faces. component. UI Input. Required ={o}; validations   Error  value is required

  • To display validation messages in the web page we have to use the following tags.
  1. <h:message>
  2. <h:message>

Where<h:message> is used to display a validation message with respect to a particular field <h:message for=”component Id value”/> Eg  <h:message for=”uname”/> Where<h:message>tag can be used to display all the validations messages of the respective fields which are available in the current page. e.g  <h:output text value=”user name”/> <h:input text id=”u name ”value=”reg bean. U name” required=”true”/> <h:message for=”u name”/>   Validate Length

  • It is applied on string data to check the length of the string data eg:<f:validate length minimum=”-”maximum=”--”/>

Where minimum attribute will take min length value and maximum attribute will take max length value

  • It the provided string data length is less than the specified minimum length value then this Validator will display the following validation message

Javax. faces. Validator, length Validator, minimum={1}:validation error value is less than allowable minimum of”{0}”

  • It we provided string data length is greater than specified maximum length value than this validate will display the following validation message.

Javax. faces. Validator. Length Validator. maximum=”{1}”: validation error. Value is greater than allowable maximum of “{0}” where{1}will take component name like uname upwd where{0} will take attribute values like ”minimum” and “maximum” on the basis of the validation msg     Eg <h:output te:1 value=”password”/> <h:input text id=”upwd” value=#{reg bean. upwd}/required=”true”> <f:validate length minimum=”6”maximum=”15”/> </h:input text> <h:message for=”upwd”/>   validate long range This Validator can be used to check wheat here the data or on input field is long data or not and the value is available in the specified range or not. Case1  If the provide value is not long data type then this Validator will display the following validation message javax. faces. Validator. lang range Validator. Type={0}; validation error: value is not at correct type Case2  If the provide value is not long data, it we provides only “minimum” attribute without “maximum” attribute and it the provided value in the input field is less than the specified “minimum” attribute value then this Validator will display the following validation message Javax. faces. Validator. Lang range Validator. Minimum= {1}: validation error: value is greater than allowable minimum of”{0}” Case3  It the provided data is long data, it we provide only ”maximum” attribute in<f:validate long range> Validator and if the provided value in input field is greater than ’maximum’ attribute then this Validator will display the following validation message. Javax. faces. validate. Lang range Validator. Maximum={1}; validation error: value is greater than allowable maximum of “{0}” Case4  It the provided value is lang data, it we provided both “minimum” and “maximum” attribute in <f:validate lang range>valid after and if the provided value in put field is less than the provided “minimum” attribute value are greater than the “maximum” attribute value then this Validator will display the following message. Javax.faces. validatore. Long range validatore. Not- in- range ={2}; validation error: specified attribute is not bet n the expected values of {0} and{1} Eg <h:output text value=”user age”/> <h:input text id=”age” value=”#{reg bean. uage}” required=”true”> <f:validate lang range minimum=”18”maximum=”30”> </h: input text > <h: message for=”uage” >    Validate double range This Validator can be used to check whether the data on an input field is double or not and the provided value is a variables within the specified range or not <f:validate double range minimum=”--”maximum=”--”> Where ”minimum” and ”maximum” attributes can be used to provided minimum and maximum values. Case1 If the provided data on the respective  field is not double data then this Validator will display the following error msg. Javax. faces. Validator. double range Validator. type={0}:validation error: value is not of the correct type. Case2 It the provided data is double data, It we provide only ”minimum” attribute without “maximum” attribute In<f:validator double range>Validator and it the provided value is less than the specified minimum value then this validator will display the following validation msg Javax.faces. validator, double range Validator MINIMUM{1}:validation error:value is less then allowable minimum of “{0}”  Case3 It the provided data is double data, It we provide only     ” maximum” attribute without “minimum ” attribute In<f:validator double range>Validator and it the provided value is greater than the specified maxim value then this validator will display this validation msg Javax. faces. Validator, double range Validator maximum{1}:validation error: value is greater than allowable maximum of “{1}”validation error: value is greater than allowable maximum of “{1}” Case4 It we provided data is double data, It we provide both attributes In<f:validator double range>validator and it the provided value is less than the specified min value or greater than validation msg. Javax. faces. validatore, double range validator  NOT-IN RANGE {2}:validation error: Specified attribute is not bet n the expected values of{0}&{1}   Eg <h:output text value=”user salary”/> <h:input text id=”usal” value=#{reg Bean. usal}”required=”true> <f:validator double range minimum=”2500”maximum=”50000”/> </h:input text> <h:message fore=”usal”/>  

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox