Package org.zkoss.bind.validator
Class BeanValidator
- java.lang.Object
-
- org.zkoss.bind.validator.AbstractValidator
-
- org.zkoss.bind.validator.BeanValidator
-
- All Implemented Interfaces:
Validator
public class BeanValidator extends AbstractValidator
A JSR 303 compatible validator for a property-binding. Notice : Before use this validator, you have to configure your environment (depends on the implementation you chosen). Here is a article Integrate ZK with JSR 303: Bean Validation talks about how to set up JSR 303 in ZK with Hibernate implementation. It validates a single property of a bean and sets the invalid message byAbstractValidator.addInvalidMessage(ValidationContext, String). Since 8.5, you can assign a self-defined message key by appending a key parameter. To use this class, you have to add@validator('beanValidator')or@validator('org.zkoss.bind.validator.BeanValidator')to the property-binding Example<grid width="600px"> <textbox id="tb" value="@bind(vm.person.firstName) @validator('beanValidator')"/> <label value="@load(vmsgs[tb])"/> </grid>
Note It doesn't supports to validate a property of a form which properties are load from a bean, if you want to validate the form property of the bean, you could use<grid width="600px"> <textbox value="@bind(vm.person.firstName) @validator('beanValidator', key='fn')"/> <label value="@load(vmsgs['fn'])"/> </grid>org.zkoss.zkmax.bind.BeanValodator- Since:
- 6.0.0
- Author:
- dennis
-
-
Constructor Summary
Constructors Constructor Description BeanValidator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Object[]getValidationInfo(ValidationContext ctx, java.lang.Object base, java.lang.String property)Get the bean class of the base object and property to validate.protected jakarta.validation.ValidatorgetValidator()protected voidhandleConstraintViolation(ValidationContext ctx, java.util.Set<jakarta.validation.ConstraintViolation<?>> violations)Handle hibernate ConstraintViolation. by default, it add to invalid messages.protected voidsort(java.util.List<jakarta.validation.ConstraintViolation<?>> viloations)Sort the violations, make multiple violation order more predictable.protected java.util.Set<jakarta.validation.ConstraintViolation<?>>validate(java.lang.Class clz, java.lang.String property, java.lang.Object value, java.lang.Class<?>[] groups)Validate the valuevoidvalidate(ValidationContext ctx)-
Methods inherited from class org.zkoss.bind.validator.AbstractValidator
addInvalidMessage, addInvalidMessage, addInvalidMessage, addInvalidMessages, addInvalidMessages, addInvalidMessages
-
-
-
-
Method Detail
-
getValidator
protected jakarta.validation.Validator getValidator()
-
validate
protected java.util.Set<jakarta.validation.ConstraintViolation<?>> validate(java.lang.Class clz, java.lang.String property, java.lang.Object value, java.lang.Class<?>[] groups)Validate the value- Parameters:
clz- the class of beanproperty- the property of beanvalue- the value to be validated.groups- the validation groups (since 8.0.0)- Returns:
- the ConstraintViolation set.
- Since:
- 6.0.1
-
validate
public void validate(ValidationContext ctx)
-
sort
protected void sort(java.util.List<jakarta.validation.ConstraintViolation<?>> viloations)
Sort the violations, make multiple violation order more predictable. By default, sort it by the constraint name.- Parameters:
viloations-
-
getValidationInfo
protected java.lang.Object[] getValidationInfo(ValidationContext ctx, java.lang.Object base, java.lang.String property)
Get the bean class of the base object and property to validate.
By default, it returns the class of base object directly.- Parameters:
ctx- the validation contextbase- the base objectproperty- the property to validate- Returns:
- a object array, the first item is the bean class of base object, 2nd item is the tuned property regards to the bean class
- Since:
- 6.0.1
-
handleConstraintViolation
protected void handleConstraintViolation(ValidationContext ctx, java.util.Set<jakarta.validation.ConstraintViolation<?>> violations)
Handle hibernate ConstraintViolation. by default, it add to invalid messages.- Parameters:
ctx-violations-- Since:
- 6.0.1
-
-