JSF Validation. can this be simplified?

I have a simple form with a bunch of fields. each one is required, and each has a different name:

  • town
  • state

when the form is submitted, I check if each field is empty and adds a unique message for each check in the context, for example:

  • city ​​required
  • condition required

I can't just use the required = true attribute in jsp because the message will be generic and that is not what I need.

I'm new to jsf, so please tell me how best to do this?

+2
source share
1 answer

Use requiredMessage attribute

 <h:inputSomething required="true" requiredMessage="Foo is required" /> 

Or use the label attribute and specify the template for the desired message.

 <h:inputSomething label="Foo" required="true" /> 

with CustomMessages.properties in the classpath that contains the custom message template

 javax.faces.component.UIInput.REQUIRED = {0} is required. 

{0} will be replaced by the value of the label attribute. You can find an overview of all the keys in the JSF specification (for example, the JSF 2.0 spec - chapter 2.5.2.4). Declare the message properties file in faces-config.xml as message-bundle :

 <application> <message-bundle>com.example.CustomMessages</message-bundle> </application> 

(assuming it's in the com.example package, you can name it whatever you want)

For additional message template templates, check the JSF specification.

+6
source

All Articles