我有一个包含3个字段的表单,并提交按钮。单击按钮时,如果在3个字段中未输入任何内容,则抛出验证消息。如果输入3个字段中的任何一个,则使用数据表处理数据并将结果显示在同一页面中。我可以为一个字段抛出验证消息,但不能为2个字段或更多字段抛出验证消息。这是我的代码。同样,如果我有一个需要传递的长值,并验证我该怎么做,因为长值无法验证为isEmpty()
或isNull()
。
这是我的代码,我希望将它与多个字段一起使用,并使用具有长值的字段进行验证。
<h:inputText id="userName" value="#{user.userName}" required="true"
requiredMessage="Please enter username" />
<h:inputText id="empId" value="#{user.empId}" required="true"
requiredMessage="Please enter Employee Id" />
<h:inputText id="acctNm" value="#{user.acctNm}" required="true"
requiredMessage="Please enter Employee Id" />
答案 0 :(得分:6)
让每个字段的required
属性检查是否存在其他字段的提交值。提交的值在参数映射#{param}
中由客户端ID作为密钥提供。
这是一个启动示例:
<h:form id="form">
<h:inputText id="field1" ... required="#{empty param['form:field2'] and empty param['form:field3']}" />
<h:inputText id="field2" ... required="#{empty param['form:field1'] and empty param['form:field3']}" />
<h:inputText id="field3" ... required="#{empty param['form:field1'] and empty param['form:field2']}" />
</h:form>
随着字段数量的增加,它变得更加难看。
或者,您可以使用OmniFaces <o:validateOneOrMore>
:
<h:form id="form">
<h:inputText id="field1" ... />
<h:inputText id="field2" ... />
<h:inputText id="field3" ... />
<o:validateOneOrMore id="oneOrMore" components="field1 field2 field3" />
<h:message for="oneOrMore" />
</h:form>
请注意,在操作方法中执行验证是错误的设计。您应该使用标准的JSF验证工具,例如requiered
,validator
,<f:validator>
和/或<f:validateXxx>
。
答案 1 :(得分:0)
至少对于iceFaces而言,另一种跨多个字段进行验证的方法,尤其是当验证比“必需”更复杂时,就是在你的支持bean中使用应用程序级验证。
@see Custom Validator in Backing Beans
此致
罗布