在我的应用程序中,我对h:inputText有问题。有些字段可能是必需的,有些字段可能不需要,我想为所有的thos字段做一个自定义验证器,并且找不到如何签入验证器类是否需要UIComponent的信息。
输入字段代码是这样的:
<h:inputText styleClass="form-control"
disabled="#{cc.attrs.bean.disableAnswerPosibility(cc.attrs.answer)}"
value="#{cc.attrs.freeAnswer}"
requiredMessage="#{msg.fieldRequired}"
a:requiredForValidator="#{cc.attrs.question.optional}"
required="#{cc.attrs.question.optional}">
<f:validator validatorId="stringValidator"/>
</h:inputText>如您所见,我尝试将此信息作为传递参数传递。它在网页上呈现为真/假,但当我试图在验证器类中获得它时,它是某种类型的对象,没有在网页源代码中显示值,而是使用来自.xhtml文件的代码。
这是代码:
Object temp = uiComponent.getPassThroughAttributes().get("requiredForValidator");我得到的是:

我只需要值-布尔值或字符串。有什么建议吗?
发布于 2015-11-05 11:38:48
在验证器的验证方法中,您可以将组件强制转换为javax.faces.component.UIInput,并按如下方式计算其required属性。
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
UIInput inputComponent = (UIInput) component;
if(inputComponent.isRequired()){
}
} 这个验证器显然只适用于UIInput组件。
https://stackoverflow.com/questions/33543029
复制相似问题