views:

34

answers:

1

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

  • city
  • state

when the form is submitted i check if each field is empty and add a unique message for each validation to the context like:

  • city is required
  • state is required

i cant simply use the required=true attribute on the jsp because the message will be generic and this is not what is needed.

I am fairly new to jsf, so please tell me of a better way to do this?

+4  A: 

Either use requiredMessage attribute

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

Or use label attribute and supply a custom required message template.

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

with a CustomMessages.properties in the classpath which contains the custom message template

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

The {0} will be substituted with the value of the label attribute. Declare the message properties file in faces-config.xml as message-bundle:

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

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

For more message template keys, check the JSF specification.

BalusC
thanks man. i will try these things now
mkoryak