其他字段验证失败时清除表单字段

时间:2011-12-08 12:14:53

标签: jsf jsf-2

当其他字段的验证失败时,我需要从验证器清除输入文本。我已尝试使用setSubmittedValue("")setValue(""),但它无效。

我想这样做的原因是因为该字段是验证码字段,如果其他字段的验证失败,页面将使用新的验证码代码呈现,我希望输入字段为空。

形式:

<h:form id="form">
   <h:inputText id="otherfieldid" required="true"
        requiredMessage="Please enter"
        validator="#{bean.validateA}"
        validatorMessage="Validation fails" />

    <h:inputText id="fieldid" required="true"
        requiredMessage="Please enter"
        validator="#{bean.validateB}"
        validatorMessage="Validation fails" />
</h:form>

豆:

@ManagedBean
@ViewScoped
public Bean {
    public void validateA(FacesContext context,
                UIComponent componentToValidate,
                Object value)
                throws ValidatorException {
        /* validation codes */
    }

    public void validateB(FacesContext context,
                UIComponent componentToValidate,
                Object value)
                throws ValidatorException {

        HtmlInputText ht = 
            (HtmlInputText) context.getViewRoot().findComponent(":form:otherfieldid");

        if (ht != null) {           
            if (ht.getValidatorMessage() != null 
                    || !"".equals(ht.getValidatorMessage())) {              
                ((HtmlInputText) componentToValidate).setSubmittedValue("");
                return;
            }
        }
        /* validation codes */
    }
}

1 个答案:

答案 0 :(得分:0)

输入字段的value="#{yourBeam.field}绑定在哪里?如果验证失败,应该可以将Bean中的此字段设置为null ...

相关问题