Search in sources :

Example 1 with FormField

use of org.kie.workbench.common.forms.processing.engine.handling.FormField 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)

Example 2 with FormField

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

the class AbstractFormEngineTest method generateFormField.

public FormField generateFormField(String fieldName, String binding, boolean validateOnChange) {
    Widget widget = mock(Widget.class);
    IsWidget isWidget = mock(IsWidget.class, withSettings().extraInterfaces(HasValue.class));
    when(isWidget.asWidget()).thenReturn(widget);
    FormField field = mock(FormField.class);
    when(field.getFieldName()).thenReturn(fieldName);
    when(field.getFieldBinding()).thenReturn(binding);
    when(field.isValidateOnChange()).thenReturn(validateOnChange);
    when(field.isBindable()).thenReturn(true);
    when(field.getWidget()).thenReturn(isWidget);
    when(field.isContentValid()).thenReturn(true);
    return field;
}
Also used : IsWidget(com.google.gwt.user.client.ui.IsWidget) Widget(com.google.gwt.user.client.ui.Widget) IsWidget(com.google.gwt.user.client.ui.IsWidget) FormField(org.kie.workbench.common.forms.processing.engine.handling.FormField) HasValue(com.google.gwt.user.client.ui.HasValue)

Example 3 with FormField

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

the class FieldStateValidatorImplTest method testValidateFieldUnexpectedWidget.

@Test
public void testValidateFieldUnexpectedWidget() {
    FormField field = mock(FormField.class);
    IsWidget widget = mock(IsWidget.class);
    when(field.getWidget()).thenReturn(widget);
    when(field.isRequired()).thenReturn(true);
    assertThatThrownBy(() -> fieldStateValidator.validate(field)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Unexpected widget type: impossible to read the value");
}
Also used : IsWidget(com.google.gwt.user.client.ui.IsWidget) FormField(org.kie.workbench.common.forms.processing.engine.handling.FormField) Test(org.junit.Test)

Aggregations

FormField (org.kie.workbench.common.forms.processing.engine.handling.FormField)3 IsWidget (com.google.gwt.user.client.ui.IsWidget)2 GWT (com.google.gwt.core.client.GWT)1 HasValue (com.google.gwt.user.client.ui.HasValue)1 Widget (com.google.gwt.user.client.ui.Widget)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 Test (org.junit.Test)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