Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation for non-required field

I have these lines of code to validate for an e-mail address:

<h:outputText value="#{labels.eMail}: " />
<p:inputText size="30" maxlength="50" validatorMessage="Please provide a valid e-mail address" value="#{personelView.eMail}">
  <f:validateRegex pattern=".+@.+\.[a-zA-Z]+" />
</p:inputText>

When I leave the e-mail address field empty and submit the form it gives validation error. Why JSf is doing this? Isn't it supposed to just validate e-mail field with the above code? I even tried adding:

required="false" 

still no good. Does anyone have any idea about this case?

like image 668
lamostreta Avatar asked May 16 '12 08:05

lamostreta


People also ask

How do you validate field only exists?

You can set a additional boolean key where value is default false. Change it to true when you modify the value in step 1. And then if the value is true for that key then apply the validation.

What do you mean by required field validation?

RequiredFieldValidator is to make an input control a required field . The input control fails validation if the value it contains does not change from its initial value when validation is performed. This prevents the user from leaving the associated input control unchanged .


1 Answers

your inputText value is being validated against your <f:validateRegex pattern=".+@.+\.[a-zA-Z]+" /> and if its value not valid you getting the error , so you need to improve your regex so it will match your pattern or accept empty string...

So wrapping it with () and adding a ? should do the job

Try

<f:validateRegex pattern="(.+@.+\.[a-zA-Z]+)?"/>
like image 154
Daniel Avatar answered Sep 19 '22 19:09

Daniel