Search in sources :

Example 1 with BeanValidation

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()));
        }
    }
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) BeanValidator(com.haulmont.cuba.gui.components.validators.BeanValidator) MetaClass(com.haulmont.chile.core.model.MetaClass) BeanValidation(com.haulmont.cuba.core.global.BeanValidation) BeanDescriptor(javax.validation.metadata.BeanDescriptor) MetaClass(com.haulmont.chile.core.model.MetaClass) KeyValueEntity(com.haulmont.cuba.core.entity.KeyValueEntity)

Example 2 with BeanValidation

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;
}
Also used : UiControllerUtils.getScreenContext(com.haulmont.cuba.gui.screen.UiControllerUtils.getScreenContext) Iterables(com.google.common.collect.Iterables) LoggerFactory(org.slf4j.LoggerFactory) CubaIcon(com.haulmont.cuba.gui.icons.CubaIcon) Icons(com.haulmont.cuba.gui.icons.Icons) Dialogs(com.haulmont.cuba.gui.Dialogs) Inject(javax.inject.Inject) Configuration(com.haulmont.cuba.core.global.Configuration) Path(javax.validation.Path) BaseAction(com.haulmont.cuba.gui.components.actions.BaseAction) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) ConstraintViolation(javax.validation.ConstraintViolation) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) Logger(org.slf4j.Logger) NotificationType(com.haulmont.cuba.gui.Notifications.NotificationType) Collection(java.util.Collection) Validator(javax.validation.Validator) Set(java.util.Set) Messages(com.haulmont.cuba.core.global.Messages) BeanValidation(com.haulmont.cuba.core.global.BeanValidation) Component(org.springframework.stereotype.Component) UiCrossFieldChecks(com.haulmont.cuba.core.global.validation.groups.UiCrossFieldChecks) Notifications(com.haulmont.cuba.gui.Notifications) ClientConfig(com.haulmont.cuba.client.ClientConfig) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) ElementKind(javax.validation.ElementKind) Entity(com.haulmont.cuba.core.entity.Entity) Path(javax.validation.Path) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator)

Example 3 with BeanValidation

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);
    }
}
Also used : BeanValidation(com.haulmont.cuba.core.global.BeanValidation) ArrayList(java.util.ArrayList) ConstraintViolation(javax.validation.ConstraintViolation) MetaClass(com.haulmont.chile.core.model.MetaClass) CompositeValidationException(com.haulmont.cuba.gui.components.CompositeValidationException) Validator(javax.validation.Validator)

Example 4 with BeanValidation

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()));
        }
    }
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) BeanValidator(com.haulmont.cuba.gui.components.validators.BeanValidator) MetaClass(com.haulmont.chile.core.model.MetaClass) BeanValidation(com.haulmont.cuba.core.global.BeanValidation) BeanDescriptor(javax.validation.metadata.BeanDescriptor) MetaClass(com.haulmont.chile.core.model.MetaClass) KeyValueEntity(com.haulmont.cuba.core.entity.KeyValueEntity)

Aggregations

BeanValidation (com.haulmont.cuba.core.global.BeanValidation)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 KeyValueEntity (com.haulmont.cuba.core.entity.KeyValueEntity)2 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)2 BeanValidator (com.haulmont.cuba.gui.components.validators.BeanValidator)2 ConstraintViolation (javax.validation.ConstraintViolation)2 Validator (javax.validation.Validator)2 BeanDescriptor (javax.validation.metadata.BeanDescriptor)2 Iterables (com.google.common.collect.Iterables)1 Preconditions.checkNotNullArgument (com.haulmont.bali.util.Preconditions.checkNotNullArgument)1 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 Messages (com.haulmont.cuba.core.global.Messages)1 UiCrossFieldChecks (com.haulmont.cuba.core.global.validation.groups.UiCrossFieldChecks)1 ComponentsHelper (com.haulmont.cuba.gui.ComponentsHelper)1 Dialogs (com.haulmont.cuba.gui.Dialogs)1 Notifications (com.haulmont.cuba.gui.Notifications)1 NotificationType (com.haulmont.cuba.gui.Notifications.NotificationType)1 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)1