This :

<h:outputLabel for="birthdate">Birthdate (yyyy-MM-dd)</h:outputLabel>
<h:inputText id="birthdate" value="#{userController.user.birthdate}">
    <f:convertDateTime pattern="yyyy-MM-dd" />
    <f:ajax event="blur" render="birthdateMessage" />
</h:inputText>
<h:message id="birthdateMessage" for="birthdate" />

works like a charm in the form. But I can't seem to find a way to move this validation at the entity:

@Temporal(TemporalType.DATE) // pattern ?
@Past
private Date birthdate;

If I remove the <f:convertDateTime pattern="yyyy-MM-dd" /> the form won't validate correctly. So should I keep this in the form - or is there a way to pass the validation in the entity (as I've done with the rest of the fields like email etc)

Bonus: is this the correct way of passing a Date to the persistent layer ? Does it take care of timezones etc ? I always use Joda in SE and IIRC Date is one of the most problematic Java classes. But when I reverse engineered the entities from the tables (using the Eclipse JPA project option Create Entities from tables) that's what I got (in the (MySQL) table the birthdate attribute is of type DATE).

EDIT: I find it better style to have the all validations at the entity than at the view or at the controller - any objections welcome

有帮助吗?

解决方案

The <f:convertDateTime> isn't a validator, but a converter. The JSR303 Bean Validation API doesn't offer converters, only validators. That's why it can't be supplanted by some JSR303 annotation.

As to time zone, JSF relies by default on GMT. If you make sure the rest of your environment is also relying on GMT, then you're safe. If you however rely on a different time zone, then you can tell JSF that on either an application-wide basis or on a converter-specific basis. Detail can be found in this answer: JSF convertDateTime renders the previous day.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top