Search in sources :

Example 16 with FieldValidator

use of org.apache.tapestry5.FieldValidator in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method validator_with_constraint.

@SuppressWarnings("unchecked")
@Test
public void validator_with_constraint() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    MessageFormatter formatter = mockMessageFormatter();
    Object inputValue = new Object();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    Integer five = 5;
    FormSupport fs = mockFormSupport();
    ValidatorMacro macro = mockValidatorMacro();
    Messages globalMessages = mockMessages();
    Map<String, Validator> map = singletonMap("minLength", validator);
    train_getConstraintType(validator, Integer.class);
    train_getFormValidationId(fs, "myform");
    train_coerce(coercer, "5", Integer.class, five);
    train_getComponentResources(field, resources);
    train_getId(resources, "fred");
    train_getContainerMessages(resources, containerMessages);
    train_alwaysNull(macro);
    train_contains(containerMessages, "myform-fred-minLength-message", false);
    train_contains(containerMessages, "fred-minLength-message", false);
    train_getMessageKey(validator, "key");
    train_getMessageFormatter(globalMessages, "key", formatter);
    train_isRequired(validator, false);
    train_getValueType(validator, Object.class);
    validator.validate(field, five, formatter, inputValue);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidator(field, "minLength", "5");
    fieldValidator.validate(inputValue);
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) FieldValidator(org.apache.tapestry5.FieldValidator) ValidatorMacro(org.apache.tapestry5.validator.ValidatorMacro) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) FormSupport(org.apache.tapestry5.services.FormSupport) ComponentResources(org.apache.tapestry5.ComponentResources) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) FieldValidator(org.apache.tapestry5.FieldValidator) Validator(org.apache.tapestry5.Validator) Test(org.testng.annotations.Test)

Example 17 with FieldValidator

use of org.apache.tapestry5.FieldValidator in project tapestry-5 by apache.

the class BeanFieldValidatorDefaultSource method createDefaultValidator.

@Override
public FieldValidator createDefaultValidator(final Field field, final String overrideId, final Messages overrideMessages, final Locale locale, final Class propertyType, final AnnotationProvider propertyAnnotations) {
    FieldValidator validator = fieldValidatorDefaultSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, propertyAnnotations);
    FieldValidator beanValidator = new BeanFieldValidator(field, validatorFactory, beanValidationGroupSource, clientValidatorSource, formSupport, environment);
    return new CompositeFieldValidator(Arrays.asList(validator, beanValidator));
}
Also used : CompositeFieldValidator(org.apache.tapestry5.internal.services.CompositeFieldValidator) FieldValidator(org.apache.tapestry5.FieldValidator) CompositeFieldValidator(org.apache.tapestry5.internal.services.CompositeFieldValidator)

Example 18 with FieldValidator

use of org.apache.tapestry5.FieldValidator in project tapestry-5 by apache.

the class BeanFieldValidatorDefaultSource method createDefaultValidator.

@Override
public FieldValidator createDefaultValidator(final ComponentResources resources, final String parameterName) {
    final Class propertyType = resources.getBoundType(parameterName);
    if (propertyType == null)
        return null;
    final Field field = (Field) resources.getComponent();
    return createDefaultValidator(field, resources.getId(), resources.getContainerMessages(), resources.getLocale(), propertyType, resources.getAnnotationProvider(parameterName));
}
Also used : Field(org.apache.tapestry5.Field)

Example 19 with FieldValidator

use of org.apache.tapestry5.FieldValidator in project tapestry-5 by apache.

the class FieldValidatorDefaultSourceImpl method createDefaultValidator.

public FieldValidator createDefaultValidator(Field field, String overrideId, Messages overrideMessages, Locale locale, Class propertyType, AnnotationProvider propertyAnnotations) {
    List<FieldValidator> validators = newList();
    for (String constraint : validationConstraintGenerator.buildConstraints(propertyType, propertyAnnotations)) {
        int equalsx = constraint.indexOf('=');
        String validatorType = equalsx > 0 ? constraint.substring(0, equalsx) : constraint;
        String constraintValue = equalsx > 0 ? constraint.substring(equalsx + 1) : null;
        FieldValidator validator = fieldValidatorSource.createValidator(field, validatorType, constraintValue, overrideId, overrideMessages, locale);
        validators.add(validator);
    }
    return validators.size() == 1 ? validators.get(0) : new CompositeFieldValidator(validators);
}
Also used : FieldValidator(org.apache.tapestry5.FieldValidator)

Example 20 with FieldValidator

use of org.apache.tapestry5.FieldValidator in project tapestry-5 by apache.

the class FieldValidatorSourceImpl method createValidator.

public FieldValidator createValidator(Field field, String validatorType, String constraintValue) {
    Component component = (Component) field;
    assert InternalUtils.isNonBlank(validatorType);
    ComponentResources componentResources = component.getComponentResources();
    String overrideId = componentResources.getId();
    // So, if you use a TextField on your EditUser page, we want to search the messages
    // of the EditUser page (the container), not the TextField (which will always be the same).
    Messages overrideMessages = componentResources.getContainerMessages();
    return createValidator(field, validatorType, constraintValue, overrideId, overrideMessages, null);
}
Also used : Messages(org.apache.tapestry5.commons.Messages) Component(org.apache.tapestry5.runtime.Component) ComponentResources(org.apache.tapestry5.ComponentResources)

Aggregations

Test (org.testng.annotations.Test)24 FieldValidator (org.apache.tapestry5.FieldValidator)22 Validator (org.apache.tapestry5.Validator)14 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)14 Messages (org.apache.tapestry5.commons.Messages)14 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)13 ComponentResources (org.apache.tapestry5.ComponentResources)12 FormSupport (org.apache.tapestry5.services.FormSupport)12 Field (org.apache.tapestry5.Field)10 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)9 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)9 Locale (java.util.Locale)4 AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)4 FieldValidatorDefaultSource (org.apache.tapestry5.services.FieldValidatorDefaultSource)4 MultipartDecoder (org.apache.tapestry5.upload.services.MultipartDecoder)4 UploadedFile (org.apache.tapestry5.upload.services.UploadedFile)4 Request (org.apache.tapestry5.http.services.Request)3 ValidationConstraintGenerator (org.apache.tapestry5.services.ValidationConstraintGenerator)3 Annotation (java.lang.annotation.Annotation)1 Binding (org.apache.tapestry5.Binding)1