【问题标题】:primefaces form multiple validation [duplicate]primefaces形成多重验证[重复]
【发布时间】:2018-03-20 15:37:48
【问题描述】:

我在验证 h:form 中的多个组件时遇到问题...我需要验证这 2 个 inputText 组件,但如果我这样做:

    <p:inputText id="actionNameInput"
                 title="TO DO"
                 value="#{repositoryBean.newActionName}"
                 label="Action name"
                 required="true"
                 requiredMessage="Action name is missing.">
        <f:validator validatorId="inputTextValidator"/>
        <f:attribute name="input1" value="Action name" />
    </p:inputText>

    <p:inputText id="identifierInput"
                 title="TO DO"
                 value="#{repositoryBean.newActionRegex}"
                 label="Identifier"
                 required="true"
                 requiredMessage="Identifier is missing.">
        <f:validator validatorId="inputTextValidator"/>
        <f:attribute name="input1" value="Identifier" />
    </p:inputText>

这里是验证器类:

@FacesValidator(value = "inputTextValidator")

public class AddActionValidatorInputText implements Validator{
    @Override
    public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException {

        if(((String)o).length() < 3){
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    uiComponent.getAttributes().get("input1")+" should be longer than 3characters.", null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            throw new ValidatorException(message);
        }
    }
}

它只会验证第一个 inputText ...我已经尝试过 this,但我无法从组件中获取值(始终为空)...我读过关于接缝面的东西,但如果我应用它,就会有一些更大的错误(就像它需要越来越多的依赖......)。我不想在我的 bean 类中验证它。

【问题讨论】:

  • 它可以与h:inputText 一起使用吗?

标签: validation jsf primefaces


【解决方案1】:

Here你可以找到问题的可能解决方案。要使值不为空,请使用 valueChangeListener 属性,如下所示:

 <p:inputText id="actionNameInput"
             title="TO DO"
             value="#{repositoryBean.newActionName}"
             label="Action name"
             required="true"
             requiredMessage="Action name is missing."
             valueChangeListener="#{repositoryBean.onNewActionNameChange}">
    <f:validator validatorId="inputTextValidator"/>
    <f:attribute name="input1" value="Action name" />
</p:inputText>

在支持 bean 中:

public void onNewActionNameChange(ValueChangeEvent event) {
    setNewActionName(event.getNewValue().toString());
}

This article关于 JSF 生命周期可以帮助你了解里面发生了什么。

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 2012-05-13
    • 1970-01-01
    • 2017-12-09
    • 2014-11-25
    • 2013-02-04
    • 2018-05-25
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多