Jak mogę wyświetlić ValidatorException i wymagane = „true” tego samego pola wejściowego w różnych elementach wiadomości

Wziąłem co następujePrzykład uruchomienia BalusC i zmodyfikowałem go, dodając przycisk wysyłania i dodatkowe wiadomości h: i usuwającf:ajax zh:inputSecret's (usuniętof:ajax przyczyną z jakiegoś powodu, gdy opuszczam pierwszyh:inputSecret natychmiast wyświetla błąd „wartość jest wymagana” dla drugiegoh:inputSecret - ale użytkownik nie ma możliwości wpisania go ... ??? <- kolejne pytanie na przyszłość? :))

OK, aby krótko opowiadać:

Próbuję dowiedzieć się, w jaki sposób można wyświetlać błędy walidacji dotyczące obu pól haseł (że hasła nie są równe) w globalnych wiadomościach h: a nie w pojedynczym h: komunikat pól haseł Chcę, aby wymagane = „true” zostanie wyświetlone w<h:message każdego pola ...

Ale teraz komunikat walidacyjny (rzucony przez mój wyjątek) i wymagany = „prawdziwy” są wyświetlane w tym samym miejscu

Oto kod:

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

I dodatkoweh:commandButton zh:messages poniżej tego kodu:

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

}
</code>

Również

Dziękuję,

Rozwiązanie (dzięki BalusC)

zmieniony

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

do

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

i

<code>String confirm = (String) component.getAttributes().get("confirm");
</code>

w

<code>UIInput confirmPasswordComponent = (UIInput) component.getAttributes().get("confirm");
String confirm = (String) confirmPasswordComponent.getSubmittedValue();
</code>

i

<code>throw new ValidatorException(new FacesMessage("Passwords are not equal."));
</code>

w

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

questionAnswers(1)

yourAnswerToTheQuestion