Steffi
Steffi

Reputation: 917

jsf multiple custom validator messages for regex

I need to validate two fields in a form: email and name. The email should match the patern [email protected] and the name should only contain letters.

I have the custom-faces-messages.properties with the message I need to display when the pattern is not matching.

javax.faces.validator.RegexValidator.NOT_MATCHED = {1}: Validation Error: Value not according to pattern ''{0}''

However, instead of {0} which displays the pattern in a pretty ugly way, I would like to display a custom text for every validation. Ex for name it should be 'Name can only contain letters' and for email 'Email does not match the pattern. Example [email protected]'

Is there a way to do this using the properties file?

Thank you in advance.

Upvotes: 1

Views: 1691

Answers (2)

StarCrafter
StarCrafter

Reputation: 461

Yes there is a way to call this from your properties file. I do that way. create a message.properties file under properties package in your project. (my path is : src/main/bla.bla.bla/bean/properties/message.properties)

now;

/src/main/webapp/WEB-INF/faces-config.xml edit this file. add those lines below

<application> 
   your.src.bla.properties.message (this is path to message.properties file)
</application>

(your.src.bla.properties.message) DONT change properties.message part as reverse.

and add this line to message.properties file :

javax.faces.validator.RegexValidator.NOT_MATCHED="Your message comes here"

now stop and start your project because it may not be hot-swapped. you'll see your message in dialog, popups on the right corner of your screen.

Upvotes: 0

Vasil Lukach
Vasil Lukach

Reputation: 3728

If your email and name fields are implemented as h:inputText, then you can use validator and validatorMessage attributes for that purpose. See JSF VDL document.

2nd (more complicated) way: create your own validator. I have own email validator and I call it in application like this:

<h:inputText id="email" label="#{msg.email}"
    value="#{registrationForm.email}"
    size="40" maxlength="80" required="true">
    <f:converter converterId="lowerCase" />
    <f:validator validatorId="email-validator" />
</h:inputText>

3rd way: check it in backing bean and display message which you want.

Upvotes: 2

Related Questions