Reputation: 946
today i tried richfaces. i wanted to create a form for user registration and i wanted to use custom validators. Here is the jsf part:
<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>
for example a custom validator:
@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);
}
}
}
it seems that these custom validators arent working inside a tag. what do i have to do to get them working. or do i have to use something different to implement a validation.
Upvotes: 0
Views: 3931
Reputation: 10569
Us the form-tag inside the rich:panel tag and it will work
<rich:panel>
<h:form>
....
</h:form>
<rich:panel>
You can also use annotations for validation using richfaces like you already tried:
<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" />
In your bean you can use this for example:
@ManagedBean
@RequestScoped
public class ValidationBean {
@Size(min = 3, max = 12)
private String name = null;
@Pattern(regexp = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$", message = "Bad email")
private String email = null;
@Min(value = 18)
@Max(value = 99)
private Integer age;
private String country;
private String jobTitle;
@AssertTrue
private boolean agreed = true;
public boolean isAgreed() {
return agreed;
}
public void setAgreed(boolean agreed) {
this.agreed = agreed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
}
<rich:validator />
Will automatically validate the user input because of these annotations. I think this makes it easier and you have less work.
Upvotes: 1