Configuring User-Defined Error Messages for Required, Maxlength Validator Rules

Configuring user-defined error messages for required, maxlength validator rules:-

In validation, xml:-

    <field property = “username” depends = “reauired”>

       <msg name = “required” key = “my.un.msg”/>

     </field> 

<field property = “password” depends = “reauired, maxlength”>

       <msg name = “required” key = “my.pass.msg”/>

      <msg name = “maxlength” key = “my.pass.max.msg”/>

<arg position = “1” key = “$ {var: maxlength}” resource = “false”/>

<var>

  <var-name> maxlength </var-name>

   <var-value> 10 </var-value>

</var>

  1. Supplying user defined error msg’s for required, maxlength valildator rules.
  2. Supplying input values required for maxlength validator rule.

In myfile.properties

  My.un.msg = username is mandatory

  My.pass.msg = Hey…….enter password

  My.pass.max.msg = u r password is too large (max of 10)

Note: – This property file need not maintain default error messages.

  • The above process of configuring user defined error msg’s for validator rules is not a recommended process bcz it kills the reusability of error messages.
  • During the testing phase of the project if you want to disable form validations temporarily then we can use validate = “false” attribute in action tag. This makes ActionServlet not to call & validate () method of form bean class in form bean life cycle. The value of this attribute in “true”.

Example: – <action-mapping>

         <action path = “register” type = “app.RegisterAction” name = “rf” input = “/register.jsp” validate = “false”>

  • This validate = “false” can disable path programmatic & declarative form validation of server side, but cannot disable programmatic & declarative form validation of client side(javascript)
  • The ActionForm class based form bean class of Struts application cannot be used for validator plug-in based server side form validation but it can be used the validator  plug-in based client side form validations.(java script)
  • Once validator plug-in is recognized by ActionServlet then the javascript code of validator rules becomes ready to come to browser window when <html: javascript> tag is executable, irrespective of whether setup is ready or not perform validator plug-in based server side form validation.
  • Since there are only 14+ validator rules in validate plug-in so they are not really sufficient for real time project development, to overcome this problem the programmer must know the process of mixing our form validation logics with validator plug-in supplied validator rules. This can be done in three approaches.
  1. By using mask validator rule
  2. By calling super.validate () method from validate () of our form bean class.

By calling valifator plug-in generated javascript function from our java script function of form page.

  1. By adding user defined validator rule in validator plug-in.

Approach (1): working with the validator rule

  • Mask is a predefined validator rule of validator plug-in that allows the programmer to write regular expression based custom form validation logic in validation.xml file.
  • Errors.invalid key holds default error msg of mask validator rule. This can be modified by using msg tag.
Errors.invalid = {0} is invalid.                    
  • Every regular expression begins with ‘^’ (char & symbol) and ends with “$“ (dollar symbol)
  • The regular expression simplifies the process of writing complex & length java code, javascript code based logics.(form validation logics)

Leave a Reply

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