Validation 如果字段是必需的,JSF如何获取验证器信息?

Validation 如果字段是必需的,JSF如何获取验证器信息?,validation,jsf,Validation,Jsf,我的应用程序中存在h:inputText问题。有些字段可能是必需的,有些则不是,我想为所有这些字段创建一个自定义的验证器,但找不到如何签入验证器类的信息:UIComponent是必需的还是否 这是输入字段代码的外观: <h:inputText styleClass="form-control" disabled="#{cc.attrs.bean.disableAnswerPosibility(cc.attrs.answer)}" value="#{cc.

我的应用程序中存在
h:inputText
问题。有些字段可能是必需的,有些则不是,我想为所有这些字段创建一个自定义的验证器,但找不到如何签入验证器类的信息: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>
下面是我得到的:


我只需要值-布尔值或字符串。有什么建议吗?

在验证器的validate方法中,您可以将组件强制转换为
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
组件。

在验证器的validate方法中,您可以将组件强制转换为
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
组件。

您是否尝试对表达式求值?这个问题令人困惑。为什么不选中
required
属性?为什么要将其复制到passthrough属性中?是否尝试计算表达式?这个问题令人困惑。为什么不选中
required
属性?为什么要把它复制到一个passthrough属性中?天哪-有时候我自己都很惊讶。。。谢谢:)我的天啊-有时候我自己也很惊讶。。。谢谢:)