Definiowanie niestandardowych ograniczeń

Niestandardowe ograniczenia są projektowane z myślą o konkretnych kryteriach weryfikacji. Takie ograniczenia można następnie definiować, łącząc adnotacje ograniczeń z listami implementacji niestandardowych mechanizmów weryfikacji.

Poniższy fragment definiuje adnotację niestandardowego ograniczenia umożliwiającego weryfikację amerykańskich kodów pocztowych:

@Documented @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy=ZipCodeValidator.class) @Size(min=5, message="{org.sample.zipcode.min_size}") @Pattern(regexp="[0-9]*") @NotNull(message="{org.sample.zipcode.cannot_be_null}") ...

Get Java EE 6. Leksykon kieszonkowy now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.