Java Reference
In-Depth Information
boolean result = idValue.matches("[0-9]{7}[JJ]");
if (!result) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary(component.getClientId()
.substring(component.getClientId().indexOf(":") + 1) +
" issue, please use the format XXXXXXXJJ");
context.addMessage(component.getClientId(), message);
// Set the component's valid attribute to false
((UIInput) component).setValid(false);
}
}
If the values passed into the validateUserId method do not match the regular expression pattern, then the
validation fails, and a message is displayed to the user. Now, let's take a look at bean validation to see how it differs from
the use of a JSF validator. Rather than implementing a separate validation method or class, an annotation is placed on
the property field of the bean that you want to validate. The following entity, org.javaee7.entity.Users , contains
bean validation constraint annotations on its fields. The same validation that is being utilized by the validateUserId
method in the previous code listing can be achieved using an @Pattern annotation.
...
@Entity
@Table(name="USERS")
public class Users implements Serializable {
@Id
@Basic(optional = false)
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private BigDecimal id;
@Pattern(regexp="[0-9]{7}[JJ]")
private String userId;
@Size(min=8, groups=Authentication.class)
private String username;
@Size(min=8, groups=Authentication.class)
@Pattern(regexp="^[a-zA-Z\\d_]{4,12}$")
private String password;
@NotNull
private String firstname;
@NotNull
private String lastname;
@NotNull()
private String email;
...
 
Search WWH ::




Custom Search