Search in sources :

Example 6 with FieldValidator

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

the class FieldValidatorDefaultSourceImpl method createDefaultValidator.

public FieldValidator createDefaultValidator(ComponentResources resources, String parameterName) {
    Class propertyType = resources.getBoundType(parameterName);
    if (propertyType == null)
        return null;
    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 7 with FieldValidator

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

the class FieldValidatorSourceImpl method createValidator.

private FieldValidator createValidator(Field field, ValidatorSpecification spec, String overrideId, Messages overrideMessages) {
    String validatorType = spec.getValidatorType();
    assert InternalUtils.isNonBlank(validatorType);
    Validator validator = validators.get(validatorType);
    if (validator == null)
        throw new IllegalArgumentException(String.format("Unknown validator type '%s'. Configured validators are %s.", validatorType, InternalUtils.join(InternalUtils.sortedKeys(validators))));
    // I just have this thing about always treating parameters as finals, so
    // we introduce a second variable to treat a mutable.
    String formValidationid = formSupport.getFormValidationId();
    Object coercedConstraintValue = computeConstraintValue(validatorType, validator, spec.getConstraintValue(), formValidationid, overrideId, overrideMessages);
    MessageFormatter formatter = findMessageFormatter(formValidationid, overrideId, overrideMessages, validatorType, validator);
    return new FieldValidatorImpl(field, coercedConstraintValue, formatter, validator, formSupport);
}
Also used : MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) FieldValidator(org.apache.tapestry5.FieldValidator) Validator(org.apache.tapestry5.Validator)

Example 8 with FieldValidator

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

the class PropertyEditor method setupEnvironment.

/**
 * Creates a {@link org.apache.tapestry5.services.PropertyEditContext} and pushes it onto the {@link
 * org.apache.tapestry5.services.Environment} stack.
 */
void setupEnvironment(final String propertyName) {
    propertyModel = model.get(propertyName);
    PropertyEditContext context = new PropertyEditContext() {

        public Messages getContainerMessages() {
            return overrides.getOverrideMessages();
        }

        public String getLabel() {
            return propertyModel.getLabel();
        }

        public String getPropertyId() {
            return propertyModel.getId();
        }

        public Class getPropertyType() {
            return propertyModel.getPropertyType();
        }

        public Object getPropertyValue() {
            return propertyModel.getConduit().get(object);
        }

        public FieldTranslator getTranslator(Field field) {
            return fieldTranslatorSource.createDefaultTranslator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
        }

        public FieldValidator getValidator(Field field) {
            return fieldValidatorDefaultSource.createDefaultValidator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
        }

        public void setPropertyValue(Object value) {
            propertyModel.getConduit().set(object, value);
        }

        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return propertyModel.getAnnotation(annotationClass);
        }
    };
    environment.push(PropertyEditContext.class, context);
    BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
    if (beanValidationContext != null) {
        beanValidationContext.setCurrentProperty(propertyName);
    }
    heartbeat.begin();
}
Also used : PropertyEditContext(org.apache.tapestry5.services.PropertyEditContext) BeanValidationContext(org.apache.tapestry5.internal.BeanValidationContext) Annotation(java.lang.annotation.Annotation)

Example 9 with FieldValidator

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

the class FieldValidatorDefaultSourceImplTest method invokes_all_constraint_generators.

@SuppressWarnings("unchecked")
@Test
public void invokes_all_constraint_generators() throws Exception {
    getMocksControl().checkOrder(true);
    ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
    FieldValidator fv1 = mockFieldValidator();
    FieldValidator fv2 = mockFieldValidator();
    FieldValidatorSource source = mockFieldValidatorSource();
    Class propertyType = Integer.class;
    AnnotationProvider provider = mockAnnotationProvider();
    String overrideId = "overrideId";
    Messages overrideMessages = mockMessages();
    Field field = mockField();
    Locale locale = Locale.ENGLISH;
    String value = "*VALUE*";
    train_buildConstraints(gen, propertyType, provider, "cons1", "cons2");
    train_createValidator(source, field, "cons1", null, overrideId, overrideMessages, locale, fv1);
    train_createValidator(source, field, "cons2", null, overrideId, overrideMessages, locale, fv2);
    fv1.validate(value);
    fv2.validate(value);
    replay();
    FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source);
    FieldValidator composite = fieldValidatorSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, provider);
    composite.validate(value);
    verify();
}
Also used : ValidationConstraintGenerator(org.apache.tapestry5.services.ValidationConstraintGenerator) Locale(java.util.Locale) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) Field(org.apache.tapestry5.Field) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidator(org.apache.tapestry5.FieldValidator) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) Test(org.testng.annotations.Test)

Example 10 with FieldValidator

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

the class FieldValidatorDefaultSourceImplTest method validator_with_constraint.

@SuppressWarnings("unchecked")
@Test
public void validator_with_constraint() throws Exception {
    ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
    FieldValidator fv = mockFieldValidator();
    FieldValidatorSource source = mockFieldValidatorSource();
    Class propertyType = Integer.class;
    AnnotationProvider provider = mockAnnotationProvider();
    String overrideId = "overrideId";
    Messages overrideMessages = mockMessages();
    Field field = mockField();
    Locale locale = Locale.ENGLISH;
    train_buildConstraints(gen, propertyType, provider, "foo=bar");
    train_createValidator(source, field, "foo", "bar", overrideId, overrideMessages, locale, fv);
    replay();
    FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source);
    FieldValidator composite = fieldValidatorSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, provider);
    assertSame(composite, fv);
    verify();
}
Also used : ValidationConstraintGenerator(org.apache.tapestry5.services.ValidationConstraintGenerator) Locale(java.util.Locale) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) Field(org.apache.tapestry5.Field) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidator(org.apache.tapestry5.FieldValidator) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) Test(org.testng.annotations.Test)

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