Search in sources :

Example 31 with FieldValidator

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

the class ValidateBindingFactoryTest method success.

@Test
public void success() {
    FieldValidatorSource source = mockFieldValidatorSource();
    ComponentResources container = mockComponentResources();
    ComponentResources component = mockComponentResources();
    FieldComponent instance = mockFieldComponent();
    Location l = mockLocation();
    FieldValidator validator = mockFieldValidator();
    String expression = "required,minLength=5";
    train_getComponent(component, instance);
    expect(source.createValidators(instance, expression)).andReturn(validator);
    replay();
    BindingFactory factory = new ValidateBindingFactory(source, interner);
    Binding binding = factory.newBinding("descrip", container, component, expression, l);
    assertSame(binding.get(), validator);
    verify();
}
Also used : Binding(org.apache.tapestry5.Binding) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) FieldComponent(org.apache.tapestry5.root.FieldComponent) FieldValidator(org.apache.tapestry5.FieldValidator) ComponentResources(org.apache.tapestry5.ComponentResources) Location(org.apache.tapestry5.commons.Location) BindingFactory(org.apache.tapestry5.services.BindingFactory) Test(org.testng.annotations.Test)

Example 32 with FieldValidator

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

the class TapestryModule method decorateFieldValidatorDefaultSource.

/**
 * Decorate FieldValidatorDefaultSource to setup the EnvironmentMessages
 * object and place it in the environment.
 * Although this could have been implemented directly in the default
 * implementation of the service, doing it
 * as service decoration ensures that the environment will be properly setup
 * even if a user overrides the default
 * service implementation.
 *
 * @param defaultSource
 *         The service to decorate
 * @param environment
 */
public static FieldValidatorDefaultSource decorateFieldValidatorDefaultSource(final FieldValidatorDefaultSource defaultSource, final Environment environment) {
    return new FieldValidatorDefaultSource() {

        public FieldValidator createDefaultValidator(Field field, String overrideId, Messages overrideMessages, Locale locale, Class propertyType, AnnotationProvider propertyAnnotations) {
            environment.push(EnvironmentMessages.class, new EnvironmentMessages(overrideMessages, overrideId));
            FieldValidator fieldValidator = defaultSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, propertyAnnotations);
            environment.pop(EnvironmentMessages.class);
            return fieldValidator;
        }

        public FieldValidator createDefaultValidator(ComponentResources resources, String parameterName) {
            EnvironmentMessages em = new EnvironmentMessages(resources.getContainerMessages(), resources.getId());
            environment.push(EnvironmentMessages.class, em);
            FieldValidator fieldValidator = defaultSource.createDefaultValidator(resources, parameterName);
            environment.pop(EnvironmentMessages.class);
            return fieldValidator;
        }
    };
}
Also used : PersistentLocale(org.apache.tapestry5.services.PersistentLocale) Locale(java.util.Locale) ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Field(org.apache.tapestry5.Field) ComponentMessages(org.apache.tapestry5.services.ComponentMessages) EnvironmentMessages(org.apache.tapestry5.internal.beaneditor.EnvironmentMessages) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) FieldValidator(org.apache.tapestry5.FieldValidator) EnvironmentMessages(org.apache.tapestry5.internal.beaneditor.EnvironmentMessages) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 33 with FieldValidator

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

the class KaptchaField method beginRender.

@SuppressWarnings("rawtypes")
boolean beginRender(MarkupWriter writer) {
    writer.element("input", "type", visible ? "text" : "password", "id", getClientId(), "class", "form-control", "name", getControlName(), "value", visible ? validationTracker.getInput(this) : "");
    resources.renderInformalParameters(writer);
    FieldValidator fieldValidator = fieldValidatorSource.createValidator(this, "required", null);
    fieldValidator.render(writer);
    writer.end();
    return false;
}
Also used : FieldValidator(org.apache.tapestry5.FieldValidator)

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