Primefaces + inputText i validator

0

Witam,
mam taki problem, zaznaczam że jestem początkujący zarówno w Javie jak i Primefaces :-)

chciałbym zrobić validator pola inputText (email). Chodzi o to że mam 2 takie pola: email1 oraz email2.
Jak stworzyć walidator (klasę walidatora) aby można było porównywać oba te pola ?

                <p:outputLabel value="Email" for="registrationFormPanel:email" />
                <p:inputText id="email" required="true"
                             requiredMessage="Email: Błąd walidacji. Wymagana wartość."
                             validator="walidatorEmail" 
                             value="#{system.uzytkownik.email}" />
                <p:message for="registrationFormPanel:email" />
                <p:outputLabel value="Powtorz email" for="registrationFormPanel:powtorzEmail" />
                <p:inputText id="powtorzEmail" required="true"
                             requiredMessage="Powtórz email: Błąd walidacji. Wymagana wartość"
                             validator="walidatorPowtorzEmail"
                             value="#{system.uzytkownik.email}" />

Próbowałem zrobić tak jak poniżej, ale niestety pole "email2" ma wartość null (mimo wpisanego tam ciągu znaków).

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String email = value.toString();   
        
        if (email.isEmpty()) {
            this.komunikat("Pole nie może być puste!");
        }
        
        // pole ":registrationFormPanel:email"
        UIComponent email2 = context.getViewRoot().findComponent(":registrationFormPanel:email");
        
        // porównanie pola 'email' z polem 'powtorzEmail'        
        if (!email.equals(email2.getValueExpression("value").toString())) {
            this.komunikat("Pole 'Email' oraz 'Powtórz email' muszą być identyczne!"+
                    email2.getValueExpression("value").getValue(null).toString());
        }        
    }

Proszę o pomoc..

1

Nie jestem pewny czy poprawnie pobierasz wartość. Spróbuj tak:

UIInput emailInput = (UIInput) context.getViewRoot().findComponent(":registrationFormPanel:email");
String email = (String) emailInput.getValue();

Upewnij się też, czy podajesz dobry identyfikator (najlepiej podejrzyj kod wyrenderowanego htmla).
Metoda "komunikat" rzuca ValidatorException?

0
Pijany Kot napisał(a):

Nie jestem pewny czy poprawnie pobierasz wartość. Spróbuj tak:

UIInput emailInput = (UIInput) context.getViewRoot().findComponent(":registrationFormPanel:email");
String email = (String) emailInput.getValue();

Upewnij się też, czy podajesz dobry identyfikator (najlepiej podejrzyj kod wyrenderowanego htmla).
Metoda "komunikat" rzuca ValidatorException?

Działa :-) !
Dzięki Pijany Kocie za podpowiedzi. Zmodyfikowałem trochę kod:

        UIInput emailInput = (UIInput)fc.getViewRoot().findComponent("registrationFormPanel:powtorzEmail");
        powtorzEmail = (String)emailInput.getSubmittedValue();

getValue() działało co 2 przesłanie formularza. Czyli za 1 wysłaniem nadal otrzymywałem null.

Jeszcze raz dziękuję.

1 użytkowników online, w tym zalogowanych: 0, gości: 1