Java EE 8 Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

Let's check each declared constraint:

    @NotBlank (message = "Name should not be blank")
@Size (min = 4, max = 10,message = "Name should be between
4 and 10 characters")
private String name;

The @NotBlank annotation will deny not only null values, but also white spaces values, and @Size speaks for itself:

    @Email (message = "Invalid e-mail format")
@NotBlank (message = "E-mail shoud not be blank")
private String email;

The @Email constraint will check the email string format:

    @PastOrPresent (message = "Created date should be past or present")
@NotNull (message = "Create date should not be null")
private LocalDate created;

@PastOrPresent will constrain LocalDate to be in the past or until the present date. It can't be in the future. 

Here we can't use @NotBlank, as there is no blank date, only null, so we avoid it using @NotNull:

    @Future (message = "Expires should be a future date")
@NotNull (message = "Expires should not be null")
private LocalDate expires;

This is the same as the last one, but constraints for a future date.

In our UI, there are two places worth a careful look:

 <h:inputText id="created" value="#{user.created}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>

...

<h:inputText id="expire" value="#{user.expires}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>

We are using convertDateTime to automatically convert the data inputted into inputText according to the dd/MM/uuuu pattern.