Class level bean validation with f:valdiateWholeBean
JSF provides a f:validateBean which bridges Bean Validation to JSF, why we need another f:validateWholeBean?
This is explained in details in the VDL docs of f:validateWholeBean.
Support multi-field validation by enabling class-level bean validation on CDI based backing beans. This feature causes a temporary copy of the bean referenced by the value attribute, for the sole purpose of populating the bean with field values already validated by and then performing class-level validation on the copy. Regardless of the result of the class-level validation, the copy is discarded.
in another word, it provides the capability of cross fields validation.
A good example is password matching check in a signup page, we have to make sure the values in password field and password confirm field are equivalent.
Create a bean validation annotation @Password.
1
@Constraint(validatedBy=PasswordValidator.class)
2
@Target(TYPE)
3
@Retention(RUNTIME)
4
@interface Password {
5
6
String message() default "Password fields must match";
7
Class[] groups() default {};
8
Class[] payload() default {};
9
}
Copied!
Constraint declares which validator will handle this annotation, here it is PasswordValidator.
1
public class PasswordValidator implements ConstraintValidator<Password, PasswordHolder> {
2
3
@Override
4
public void initialize(Password constraintAnnotation) { }
5
6
@Override
7
public boolean isValid(PasswordHolder value, ConstraintValidatorContext context) {
8
boolean result;
9
10
result = value.getPassword1().equals(value.getPassword2());
11
12
return result;
13
}
14
15
}
Copied!
ConstraintValidator accept two parameters, the validator annotation, and the type(PasswordHolder) will be applied.
PasswordHolder is an interface which holds the values of two password fields.
1
public interface PasswordHolder {
2
3
String getPassword1();
4
String getPassword2();
5
}
Copied!
Create a backing bean to put all together.
1
@Named
2
@RequestScoped
3
@Password(groups = PasswordValidationGroup.class)
4
public class BackingBean implements PasswordHolder, Cloneable {
5
6
private String password1;
7
8
private String password2;
9
10
public BackingBean() {
11
password1 = "";
12
password2 = "";
13
}
14
15
@Override
16
protected Object clone() throws CloneNotSupportedException {
17
BackingBean other = (BackingBean) super.clone();
18
other.setPassword1(this.getPassword1());
19
other.setPassword2(this.getPassword2());
20
return other;
21
}
22
23
@NotNull(groups = PasswordValidationGroup.class)
24
@Size(max = 16, min = 8, message = "Password must be between 8 and 16 characters long",
25
groups = PasswordValidationGroup.class)
26
@Override
27
public String getPassword1() {
28
return password1;
29
}
30
31
public void setPassword1(String password1) {
32
this.password1 = password1;
33
}
34
35
@NotNull(groups = PasswordValidationGroup.class)
36
@Size(max = 16, min = 8, message = "Password must be between 8 and 16 characters long",
37
groups = PasswordValidationGroup.class)
38
@Override
39
public String getPassword2() {
40
return password2;
41
}
42
43
public void setPassword2(String password2) {
44
this.password2 = password2;
45
}
46
47
}
Copied!
We apply Password validation on the backingBean, it is a class level validation.
Create a simple facelets template.
1
<h:panelGroup id="messageFromInputBox">
2
<h:messages />
3
</h:panelGroup>
4
<h:form>
5
<h:panelGrid columns="2">
6
7
<h:outputText value="Password" />
8
<h:inputSecret id="password1" value='#{backingBean.password1}' required="true">
9
<f:validateBean validationGroups="javax.validation.groups.Default,com.hantsylabs.example.ee8.jsf.PasswordValidationGroup" />
10
</h:inputSecret>
11
12
<h:outputText value="Password again" />
13
<h:inputSecret id="password2" value='#{backingBean.password2}' rendered="true">
14
<f:validateBean validationGroups="javax.validation.groups.Default,com.hantsylabs.example.ee8.jsf.PasswordValidationGroup" />
15
</h:inputSecret>
16
17
</h:panelGrid>
18
19
<f:validateWholeBean value='#{backingBean}'
20
validationGroups="com.hantsylabs.example.ee8.jsf.PasswordValidationGroup" />
21
<div>
22
<h:commandButton
23
id="validtePassword"
24
value="Validate Password">
25
<f:ajax execute="@form" render=":messageFromInputBox" />
26
</h:commandButton>
27
</div>
28
</h:form>
Copied!
f:validateWholeBean accept a value attribute to set the backing bean.
NOTE, we use a PasswordValidationGroup group to differentiate varied validations.
PasswordValidationGroup is just an interface.
1
public interface PasswordValidationGroup {
2
3
}
Copied!
Run the project on Glassfish v5, open your browser and navigate to http://localhost:8080/jsf-validwholebean/.
Try to input passwords, if its length is less than 8, submit form, it will display erros like.
length validation error
If the length is valid, and the two password are not matched, it will trigger the @Password validator.
password validator error
Grab the source codes from my GitHub account, and have a try.
Last modified 8mo ago
Copy link