开发者

JSF validation. can this be simplified?

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?


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. You can find an overview of all keys in the JSF specification (e.g. 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 that it's in the package com.example, you can name it whatever you want)

For more message template keys, check the JSF specification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜