Ich habe auf meiner Seite (die inzwischen auf Primeface basiert) unter anderem 2 Felder
[ul]
[li]Email / Benutzername
[/li][li]Emailwiederholung
[/li][/ul]
die betreffende Stelle (Ausschnittsweise)
[XML]
<p:row>
<p:column>
<p:outputLabel for=“email” value="#{msg.email}" />
</p:column>
<p:column colspan=“3”>
<p:inputText id=“email” value="#{userBean.user.email}" required=“true” size=“60”
requiredMessage="#{msg.error_email_required}"
validatorMessage="#{msg.error_email_invalid}" >
<f:validateRegex pattern="^[_A-Za-z0-9-+]+(.[_A-Za-z0-9-]+)@[A-Za-z0-9-]+(.[A-Za-z0-9]+)(.[A-Za-z]{2,})$" />
</p:inputText>
<p:watermark for=“email” value="#{msg.watermark_email}" id=“email_watermark” />
<p:message for=“email” />
</p:column>
</p:row>
<p:row>
<p:column>
<p:outputLabel for=“emailrepeat” value="#{msg.emailrepeat}" />
</p:column>
<p:column colspan=“3”>
<p:inputText id=“emailrepeat” value="#{userBean.emailrepeat}" required=“true” size=“60”
requiredMessage="#{msg.emailrepeat_required}" >
<f:validator validatorId=“duplicateFieldValidator” />
<f:attribute name=“field1Id” value="#{component.parent.parent.clientId}:email" />
</p:inputText>
<p:watermark for=“emailrepeat” value="#{msg.emailrepeat}" id=“emailrepeat_watermark” />
<p:message for=“emailrepeat” />
</p:column>
</p:row>
[/XML]
die Validation-Klasse sieht wie folgt aus
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
/**
* the validation for duplicate field in JSF
*
* @author Thomas
*/
@FacesValidator(value="duplicateFieldValidator")
public class DuplicateFieldValidation implements Validator {
/**
* validate a duplicate Field
*
* @param context
* @param component
* @param value
* @throws ValidatorException
*/
@Override
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
// Obtain the client ID of the first field from f:attribute.
System.out.println(component.getFamily());
String field1Id = (String) component.getAttributes().get("field1Id");
// Find the actual JSF component for the client ID.
UIInput textInput = (UIInput) context.getViewRoot().findComponent(field1Id);
if (textInput == null)
throw new IllegalArgumentException(String.format("Unable to find component with id %s",field1Id));
// Get its value, the entered text of the first field.
String field1 = (String) textInput.getValue();
// Cast the value of the entered text of the second field back to String.
String confirm = (String) value;
// Check if the first text is actually entered and compare it with second text.
if (field1 != null && field1.length() != 0 && !field1.equals(confirm)) {
throw new ValidatorException(new FacesMessage("E-mail addresses are not equal."));
}
}
}
Beim Abschicken erhalte ich allerdings einen Fehler. Dieses ist auch ganz klar. Es liegt an “component.parent.parent” in der xhtml.
Ich weis nur nicht wie ich auf das id=“email” - Textfeld komme. Wer könnte mir hier bitte weiterhelfen.