验证jsf数据表中的输入字段
我在jsf数据表中有一个输入字段,我正在验证并添加错误消息,但并没有显示错误消息。验证jsf数据表中的输入字段并添加消息的最佳方法是什么验证jsf数据表中的输入字段,jsf,datatable,Jsf,Datatable,我在jsf数据表中有一个输入字段,我正在验证并添加错误消息,但并没有显示错误消息。验证jsf数据表中的输入字段并添加消息的最佳方法是什么 <h:inputTextarea id="textarea1" styleClass="inputTextarea" value="#{varreviewList.comment}"> <f:attribute name="msgRef" value="Valid comment is Required."/> </h:inpu
<h:inputTextarea id="textarea1" styleClass="inputTextarea" value="#{varreviewList.comment}">
<f:attribute name="msgRef" value="Valid comment is Required."/>
</h:inputTextarea>
<h:message styleClass="message" for="textarea1" id="textarea1Msg"></h:message>
任何帮助都很好
谢谢,
Sujit代码看起来很好,假设它们都放在
中(但不一定是同一列),所以问题出在其他地方
如果您毕竟只是想验证值是否已填充,那么您只需要将required=“true”
添加到相关组件中
<h:inputSomething required="true" />
…或在faces config.xml
中的应用程序的消息包中提供自定义messages.properties
,并使用以下行
javax.faces.component.UIInput.REQUIRED = Please enter {0}.
…其中,{0}
是可由控制的(因为只有JSF 1.2,否则它就是客户端ID
)
…并通过validator
属性将其附加到输入组件
<h:inputSomething validator="myValidator" />
在实现的validate()
方法中,只要在需要时使用所需的函数即可
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value does not match some condition) {
throw new ValidatorException(new FacesMessage("Please enter valid value"));
}
}
然后,它将自动显示在关联的消息组件中。这在数据表中为我工作:
<ice:inputText id="inputTextt" field="#{bean[fieldValue]}" required="true"/>
<ice:message for="inputTextt"></ice:message>
Hi BalusC,感谢您回复我编写的自定义验证器,但我的验证器没有被调用。当我点击命令按钮时,它开始工作。“我的页面”字段中不需要任何内容。我需要在某些条件下检查验证。我在faces配置中注册了验证器,并在jsf页面中指定了类似的内容。仅当值不为null或空时,才会调用附加的验证器。对于该零件,通常使用required
属性。您可能需要详细说明确切的功能需求,以便我们能够给出更合适的答案。也许您想在特定条件/依赖项下进行验证?再次感谢我让验证程序工作。我的要求是我必须检查相关性并在datatable行中显示错误消息。我能够添加错误消息并全局显示消息,但不能在datatable行中显示。示例:如果需要第5行注释,则我必须在datatable的第5行第3列(注释位于第3列)中显示错误消息。我已经显示了错误消息,但未显示该消息。感谢BalusC的回复,但在我看来,在datatable h中:与inputfields的消息关联无法正常工作。我尝试使用自定义验证器和服务器端验证。我正在正确添加消息,但当它呈现消息时,消息不会显示。再次感谢您的评论。
<h:inputSomething validator="myValidator" />
<h:inputSomething>
<f:validator validatorId="myValidator" />
</h:inputSomething>
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value does not match some condition) {
throw new ValidatorException(new FacesMessage("Please enter valid value"));
}
}
<ice:inputText id="inputTextt" field="#{bean[fieldValue]}" required="true"/>
<ice:message for="inputTextt"></ice:message>