如何显示ValidatorException和required ="true"?不同消息元素中的相同输入字段 [英] How can I display ValidatorException and required="true" of same input field in different messages elements

查看:243
本文介绍了如何显示ValidatorException和required ="true"?不同消息元素中的相同输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采用了以下 BalusC启动示例,并通过添加提交按钮和其他h:messages对其进行了一些修改.从h:inputSecret's中删除f:ajax(由于某种原因删除了f:ajax原因,当我离开第一个h:inputSecret时,第二个h:inputSecret立即显示"value is required"错误-但用户没有有机会在...中键入它... ???<-另一个未来的问题?:))

I took the following BalusC kickoff example and modified it a bit by adding a submit button and additional h:messages and removing the f:ajax from the h:inputSecret's (removed the f:ajax cause for some reason when I leave the first h:inputSecret it immediately displays "value is required" error for the second h:inputSecret - but the user haven't got the chance to type it in... ??? <- another future question ?:) )

好,简而言之:

我试图弄清楚如何在全局h:messages中而不是在单个h:message中显示关于两个密码字段(密码不相等)的验证错误. 我确实希望在每个字段的<h:message中显示required ="true" ...

I'm trying to figure out how can display the validation errors regarding the both password fields(that the passwords are not equal) in the global h:messages and not in the individual h:message of the password fields I do want that the required="true" will be displayed in the <h:message of each field...

但是现在,验证消息(我的例外抛出)和required ="true"都显示在同一位置

But right now the validation message (thrown by my exception) and the required="true" are being displayed in the same place

这是代码:

<h:outputLabel for="password" value="Password:" />
<h:inputSecret id="password" value="#{bean.password}" required="true">
    <f:validator validatorId="confirmPasswordValidator" />
    <f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
</h:inputSecret>
<h:message id="m_password" for="password" />

<h:outputLabel for="confirm" value="Password (again):" />
<h:inputSecret id="confirm" binding="#{confirmPassword}" required="true">
</h:inputSecret>
<h:message id="m_confirm" for="confirm" />

以及该代码下方带有h:messages的其他h:commandButton:

And additional h:commandButton with h:messages below that code :

<h:commandButton value="doSomething" action="#{myBean.myAction}">
    <f:ajax execute="password confirm" render="m_password m_confirm"></f:ajax>
</h:commandButton>
<h:messages globalOnly="true" styleClass="validation_value_required"/>

@FacesValidator("confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String password = (String) value;
        String confirm = (String) component.getAttributes().get("confirm");

        if (password == null || confirm == null) {
            return; // Just ignore and let required="true" do its job.
        }

        if (!password.equals(confirm)) {
            throw new ValidatorException(new FacesMessage("Passwords are not equal."));
        }
    }

}

谢谢,

解决方案(感谢BalusC)

已更改

<f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />

<f:attribute name="confirm" value="#{confirmPassword}" />

String confirm = (String) component.getAttributes().get("confirm");

进入

UIInput confirmPasswordComponent = (UIInput) component.getAttributes().get("confirm");
String confirm = (String) confirmPasswordComponent.getSubmittedValue();

throw new ValidatorException(new FacesMessage("Passwords are not equal."));

进入

context.addMessage(null, new FacesMessage("Passwords are not equal."));
context.validationFailed();
((UIInput) component).setValid(false);
confirmPasswordComponent.setValid(false);
return;

推荐答案

如果特定组件上的Validator抛出ValidatorException,则其FacesMessage将自动与Validator所在的组件相关联被调用.

If a Validator on a particular component throws a ValidatorException, then its FacesMessage will automatically be associated with the component on which the Validator is invoked.

您需要在null客户端ID上手动添加FacesMessage,以使其最终出现在<h:messages globalOnly="true">中.您还需要在FacesContext上手动设置validationFailed(),以便JSF不会更新模型值或调用操作.如果有必要(尽管推荐),您还需要手动将组件标记为无效,以便任何适当的侦听器/树状访问者(例如,用于突出显示)都将考虑在内.

You need to manually add the FacesMessage on a null client ID so that it end up in <h:messages globalOnly="true">. You also need to manually set validationFailed() on FacesContext so that JSF won't update the model values nor invoke the action. If necessary (though recommended), you also need to manually mark the components as invalid so that any appropriate listeners/tree-visitors (e.g. for highlighting) will take this into account.

if (!password.equals(confirm)) {
    context.addMessage(null, new FacesMessage("Passwords are not equal."));
    context.validationFailed();
    ((UIInput) component).setValid(false);
    confirmPasswordComponent.setValid(false); // You'd need to pass it as component instead of as its submitted value in f:attribute.
}

顺便说一句, OmniFaces 项目具有一个<o:validateEqual>组件,这应该使它不再那么乏味.另请参见展示案例示例.

By the way, the OmniFaces project has an <o:validateEqual> component which should make this less tedious. See also showcase example.

这篇关于如何显示ValidatorException和required ="true"?不同消息元素中的相同输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆