Search in sources :

Example 1 with NESTED_PROPERTY_SEPARATOR

use of org.kie.workbench.common.forms.processing.engine.handling.FormValidator.NESTED_PROPERTY_SEPARATOR in project kie-wb-common by kiegroup.

the class DefaultModelValidator method validate.

@Override
public boolean validate(Collection<FormField> fields, MODEL model) {
    boolean isValid = true;
    if (model instanceof BindableProxy) {
        model = ((BindableProxy<MODEL>) model).deepUnwrap();
    }
    try {
        Set<ConstraintViolation<Object>> result = validator.validate(model);
        for (ConstraintViolation<Object> constraintViolation : result) {
            Optional<FormField> found = fields.stream().filter(formField -> {
                Path propertyPath = constraintViolation.getPropertyPath();
                String propertyName = propertyPath.iterator().next().getName();
                if (checkBinding(formField, propertyName)) {
                    return true;
                }
                propertyName = propertyPath.toString().replace(".", NESTED_PROPERTY_SEPARATOR);
                if (checkBinding(formField, propertyName)) {
                    return true;
                }
                return false;
            }).findFirst();
            if (!found.isPresent()) {
                continue;
            }
            FormField formField = found.get();
            if (formField == null) {
                continue;
            }
            isValid = false;
            formField.showError(constraintViolation.getMessage());
        }
    } catch (IllegalArgumentException ex) {
        GWT.log("Error trying to validate model: model does not any validation constraint. ");
    }
    return isValid;
}
Also used : NESTED_PROPERTY_SEPARATOR(org.kie.workbench.common.forms.processing.engine.handling.FormValidator.NESTED_PROPERTY_SEPARATOR) Default(javax.enterprise.inject.Default) FormField(org.kie.workbench.common.forms.processing.engine.handling.FormField) Collection(java.util.Collection) Set(java.util.Set) Validator(javax.validation.Validator) GWT(com.google.gwt.core.client.GWT) Inject(javax.inject.Inject) ModelValidator(org.kie.workbench.common.forms.processing.engine.handling.ModelValidator) Dependent(javax.enterprise.context.Dependent) Optional(java.util.Optional) Path(javax.validation.Path) BindableProxy(org.jboss.errai.databinding.client.BindableProxy) ConstraintViolation(javax.validation.ConstraintViolation) Path(javax.validation.Path) BindableProxy(org.jboss.errai.databinding.client.BindableProxy) ConstraintViolation(javax.validation.ConstraintViolation) FormField(org.kie.workbench.common.forms.processing.engine.handling.FormField)

Aggregations

GWT (com.google.gwt.core.client.GWT)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Dependent (javax.enterprise.context.Dependent)1 Default (javax.enterprise.inject.Default)1 Inject (javax.inject.Inject)1 ConstraintViolation (javax.validation.ConstraintViolation)1 Path (javax.validation.Path)1 Validator (javax.validation.Validator)1 BindableProxy (org.jboss.errai.databinding.client.BindableProxy)1 FormField (org.kie.workbench.common.forms.processing.engine.handling.FormField)1 NESTED_PROPERTY_SEPARATOR (org.kie.workbench.common.forms.processing.engine.handling.FormValidator.NESTED_PROPERTY_SEPARATOR)1 ModelValidator (org.kie.workbench.common.forms.processing.engine.handling.ModelValidator)1