0
今天我嘗試了richfaces。我想創建一個用戶註冊表格 ,我想使用自定義驗證器。下面是JSF的部分:JSF Richfaces和自定義驗證
<ui:composition template="/templates/master.xhtml" >
<ui:define name="content">
<center>
<h:outputText value="New User Registration" />
</center>
<h:form>
<rich:panel>
<h:panelGrid columns="3" >
<h:outputText value="Username:" />
<h:inputText id="username" value="#{registerService.userName}">
<!-- <f:validator validatorId="userNameValidator"/> -->
<rich:validator />
</h:inputText>
<h:message id="m_username" for="username" style="color:red" />
<h:outputText value="Password:" />
<h:inputSecret id="password" value="#{registerService.password}">
<f:validator validatorId="passwordValidator" />
</h:inputSecret>
<h:message id="m_password" for="password" style="color:red" />
<h:outputText value="Confirm Password:" />
<h:inputSecret id="confirmPassword" value="#{registerService.confirmPassword}">
<f:validator validatorId="passwordConfirmValidator" />
<f:attribute name="passwordId" value="#{component.parent.parent.clientId}:password" />
</h:inputSecret>
<h:message id="m_confirmPassword" for="confirmPassword" style="color:red" />
<h:outputText value="Lastname:" />
<h:inputText id="lastname" value="#{registerService.lastName}">
<f:validator validatorId="nameValidator" />
</h:inputText>
<h:message id="m_lastname" for="lastname" style="color:red" />
<h:outputText value="Firstname:" />
<h:inputText id="firstname" value="#{registerService.firstName}">
<f:validator validatorId="nameValidator" />
</h:inputText>
<h:message id="m_firstname" for="firstname" style="color:red" />
<h:outputText value="Date of Birth: " />
<rich:calendar value="#{registerService.birthDate}" />
<h:outputText value=""/>
<h:outputText value="E-Mail:" />
<h:inputText id="email" value="#{registerService.eMail}" >
<f:validator validatorId="emailValidator" />
</h:inputText>
<h:message id="m_email" for="email" style="color:red" />
<h:outputText value="Confirm E-Mail:" />
<h:inputText id="confirmEmail" value="#{registerService.confirmEMail}">
<f:validator validatorId="emailConfirmValidator" />
<f:attribute name="emailId" value="#{component.parent.parent.clientId}:email" />
</h:inputText>
<h:message id="m_confirmEmail" for="confirmEmail" style="color:red" />
</h:panelGrid>
<h:commandButton type="submit" value="Submit" action="#{registerService.addUser}" >
<f:ajax execute="@form" render="m_username m_password m_confirmPassword m_lastname m_firstname m_email m_confirmEmail" />
</h:commandButton>
<h:commandButton type="reset" value="Reset" />
</rich:panel>
</h:form>
</ui:define>
</ui:composition>
例如一個自定義的驗證:
@FacesValidator(value="emailValidator")
public class EmailValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String email = (String) value;
FacesMessage message = null;
if(email == null || email.isEmpty()) {
message = new FacesMessage ("Please enter a E-Mail!", "Email Validation Error");
message.setDetail("You havent entered a valid E-Mail yet!");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
if(!email.contains("@")) {
message = new FacesMessage ("Invalid E-Mail!", "Email Validation Error");
message.setDetail("E-Mail does not contain '@'");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
Matcher matcher = Regex.EMAIL_PATTERN_COMPILED.matcher(email);
if(!matcher.matches()) {
message = new FacesMessage ("Invalid E-Mail!", "Email Validation Error");
message.setDetail("E-Mail contains forbidden characters or does not match the E-Mail pattern!");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
if(email.length() > 64) {
message = new FacesMessage ("Invalid E-Mail!", "Email Validation Error");
message.setDetail("E-Mail is to long (max. 64 characters)!");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
}
似乎這些自定義驗證的arent標籤內工作。 我必須做些什麼才能讓他們工作。或者我必須使用不同的 來實施驗證。