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