10

Вопрос по jsf, validation, jsf-2 – Как я могу отобразить ValidatorException и required = «true» одного и того же поля ввода в разных элементах сообщений

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceededf:ajaxError: User Rate Limit Exceededh:inputSecret'sError: User Rate Limit Exceededf:ajaxError: User Rate Limit Exceededh:inputSecretError: User Rate Limit Exceededh:inputSecretError: User Rate Limit Exceeded

OK, to make long story short:

Error: User Rate Limit Exceeded<h:messageError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

<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" />

Error: User Rate Limit Exceededh:commandButtonError: User Rate Limit Exceededh:messagesError: User Rate Limit Exceeded

<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."));
        }
    }

}

Также

Error: User Rate Limit Exceeded

Solution (Thanks to BalusC)

Error: User Rate Limit Exceeded

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

в

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

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

context.addMessage(null, new FacesMessage("Passwords are not equal."));
context.validationFailed();
((UIInput) component).setValid(false);
confirmPasswordComponent.setValid(false);
return;
  • Вам нужно передать его как компонент, а не как его переданное значение в атрибуте f :. Как?

    от
  • 8

    Error: User Rate Limit Exceeded

    ValidatorValidatorExceptionFacesMessageValidator

    FacesMessagenull<h:messages globalOnly="true">validationFailed()FacesContext

    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.
    }
    

    <o:validateEqual>.