ajax - Optional field with minimum length validation in JSF -
i have registration form optional field if enter value should more 2 characters. how skip validation if user leaves field blank?
<h:inputtext id ="fooid" maxlength="50" tabindex="4" value="#{registrationbean.foo}" validatormessage="please enter 2 or more characters"> <f:validateregex pattern="^[a-za-z-_./\s]{2,50}$"/> <f:ajax event="blur" render="foopanel" /> </h:inputtext>
any appreciated.
it works intented if add following context parameter:
<context-param> <param-name>javax.faces.interpret_empty_string_submitted_values_as_null</param-name> <param-value>true</param-value> </context-param>
this tell jsf interpret empty string submitted values null
. if value null
, required="true"
validator triggered , other validators not. additional advantage keeps model free of cluttered empty strings when enduser didn't fill out inputs.
if not option reason, e.g. when model incorrectly relying on empty strings, best alternative check in regex empty string well:
<f:validateregex pattern="^$|^[a-za-z-_./\s]{2,50}$" />
Comments
Post a Comment