use of com.haulmont.cuba.core.global.BeanValidation in project cuba by cuba-platform.
the class WebAbstractField method initBeanValidator.
protected void initBeanValidator() {
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
MetaClass propertyEnclosingMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
Class enclosingJavaClass = propertyEnclosingMetaClass.getJavaClass();
if (enclosingJavaClass != KeyValueEntity.class && !DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
BeanValidation beanValidation = AppBeans.get(BeanValidation.NAME);
javax.validation.Validator validator = beanValidation.getValidator();
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(enclosingJavaClass);
if (beanDescriptor.isBeanConstrained()) {
addValidator(new BeanValidator(enclosingJavaClass, metaProperty.getName()));
}
}
}
use of com.haulmont.cuba.core.global.BeanValidation in project cuba by cuba-platform.
the class ScreenValidation method validateCrossFieldRules.
/**
* Validate cross-field BeanValidation rules.
*
* @param origin screen controller
* @param item item to validate
* @return validation errors
*/
public ValidationErrors validateCrossFieldRules(@SuppressWarnings("unused") FrameOwner origin, Entity item) {
ValidationErrors errors = new ValidationErrors();
Validator validator = beanValidation.getValidator();
Set<ConstraintViolation<Entity>> violations = validator.validate(item, UiCrossFieldChecks.class);
violations.stream().filter(violation -> {
Path propertyPath = violation.getPropertyPath();
Path.Node lastNode = Iterables.getLast(propertyPath);
return lastNode.getKind() == ElementKind.BEAN;
}).forEach(violation -> errors.add(violation.getMessage()));
return errors;
}
use of com.haulmont.cuba.core.global.BeanValidation in project cuba by cuba-platform.
the class BeanValidator method validate.
@Override
public void validate(Object value) throws ValidationException {
BeanValidation beanValidation = AppBeans.get(BeanValidation.NAME);
Validator validator = beanValidation.getValidator();
Class[] groups = this.validationGroups;
if (groups == null || groups.length == 0) {
groups = new Class[] { Default.class, UiComponentChecks.class };
}
@SuppressWarnings("unchecked") Set<ConstraintViolation> violations = validator.validateValue(beanClass, beanProperty, value, groups);
if (!violations.isEmpty()) {
List<CompositeValidationException.ViolationCause> causes = new ArrayList<>();
for (ConstraintViolation violation : violations) {
causes.add(new BeanValidationViolationCause(violation));
}
String validationMessage = this.validationErrorMessage;
if (validationMessage == null) {
validationMessage = getDefaultErrorMessage();
}
throw new CompositeValidationException(validationMessage, causes);
}
}
use of com.haulmont.cuba.core.global.BeanValidation in project cuba by cuba-platform.
the class DesktopAbstractField method initBeanValidator.
protected void initBeanValidator() {
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
MetaClass propertyEnclosingMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
Class enclosingJavaClass = propertyEnclosingMetaClass.getJavaClass();
if (enclosingJavaClass != KeyValueEntity.class && !DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
BeanValidation beanValidation = AppBeans.get(BeanValidation.NAME);
javax.validation.Validator validator = beanValidation.getValidator();
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(enclosingJavaClass);
if (beanDescriptor.isBeanConstrained()) {
addValidator(new BeanValidator(enclosingJavaClass, metaProperty.getName()));
}
}
}
Aggregations