首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果需要字段,JSF如何获取验证器信息?

如果需要字段,JSF如何获取验证器信息?
EN

Stack Overflow用户
提问于 2015-11-05 11:08:24
回答 1查看 1.6K关注 0票数 1

在我的应用程序中,我对h:inputText有问题。有些字段可能是必需的,有些字段可能不需要,我想为所有的thos字段做一个自定义验证器,并且找不到如何签入验证器类是否需要UIComponent的信息。

输入字段代码是这样的:

代码语言:javascript
运行
复制
<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文件的代码。

这是代码:

代码语言:javascript
运行
复制
Object temp = uiComponent.getPassThroughAttributes().get("requiredForValidator");

我得到的是:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-05 11:38:48

在验证器的验证方法中,您可以将组件强制转换为javax.faces.component.UIInput,并按如下方式计算其required属性。

代码语言:javascript
运行
复制
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        UIInput inputComponent = (UIInput) component;
        if(inputComponent.isRequired()){

        }
    } 

这个验证器显然只适用于UIInput组件。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33543029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档