Search in sources :

Example 1 with Validator

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

the class UploadTest method begin_render_writes_input_tag.

@Test
public void begin_render_writes_input_tag() throws Exception {
    MarkupWriter writer = createMarkupWriter();
    writer.element("form");
    FormSupport formSupport = mockFormSupport();
    ComponentResources resources = mockComponentResources();
    FieldValidator validator = mockFieldValidator();
    Request request = mockRequest();
    train_isXHR(request, false);
    formSupport.setEncodingType(Upload.MULTIPART_ENCTYPE);
    validator.render(writer);
    resources.renderInformalParameters(writer);
    replay();
    Upload component = new Upload(null, null, null, null, resources, null);
    component.injectDecorator(new BaseValidationDecorator()).injectFormSupport(formSupport).injectFieldValidator(validator).injectRequest(request);
    component.beginRender(writer);
    Element element = writer.getElement();
    assertNotNull(element);
    assertEquals(element.getName(), "input");
    assertEquals(element.getAttribute("type"), "file");
    // assertEquals(element.getAttribute("name"),null);
    // assertEquals(element.getAttribute("id"),null);
    verify();
}
Also used : Element(org.apache.tapestry5.dom.Element) Request(org.apache.tapestry5.http.services.Request) FormSupport(org.apache.tapestry5.services.FormSupport) Test(org.testng.annotations.Test)

Example 2 with Validator

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

the class UploadTest method validation_decorator_invoked_inside_begin_render.

@Test
public void validation_decorator_invoked_inside_begin_render() throws Exception {
    getMocksControl().checkOrder(true);
    ComponentResources resources = mockComponentResources();
    Upload component = new Upload(null, null, null, null, resources, null);
    MarkupWriter writer = createMarkupWriter();
    writer.element("form");
    FieldValidator validator = mockFieldValidator();
    Request request = mockRequest();
    FormSupport formSupport = mockFormSupport();
    formSupport.setEncodingType(Upload.MULTIPART_ENCTYPE);
    component.injectFormSupport(formSupport).injectRequest(request);
    ValidationDecorator decorator = mockValidationDecorator();
    component.injectDecorator(decorator).injectFieldValidator(validator);
    validator.render(writer);
    resources.renderInformalParameters(writer);
    decorator.insideField(component);
    train_isXHR(request, false);
    replay();
    component.beginRender(writer);
    verify();
}
Also used : Request(org.apache.tapestry5.http.services.Request) FormSupport(org.apache.tapestry5.services.FormSupport) Test(org.testng.annotations.Test)

Example 3 with Validator

use of org.apache.tapestry5.Validator 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 4 with Validator

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

the class EmailTest method input_mismatch.

@Test
public void input_mismatch() throws Exception {
    Field field = mockField();
    MessageFormatter formatter = mockMessageFormatter();
    Html5Support html5Support = mockHtml5Support();
    replay();
    Email validator = new Email(null, html5Support);
    try {
        validator.validate(field, null, formatter, "invalid_email");
        unreachable();
    } catch (ValidationException ex) {
    }
    try {
        validator.validate(field, null, formatter, "@mail.com");
        unreachable();
    } catch (ValidationException ex) {
    }
    // TAP5-2282
    try {
        validator.validate(field, null, formatter, "aaa@bbb/cc");
        unreachable();
    } catch (ValidationException ex) {
    }
    verify();
}
Also used : Field(org.apache.tapestry5.Field) Html5Support(org.apache.tapestry5.services.Html5Support) ValidationException(org.apache.tapestry5.ValidationException) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) Test(org.testng.annotations.Test)

Example 5 with Validator

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

the class MaxLengthTest method short_enough.

@Test
public void short_enough() throws Exception {
    Field field = mockField();
    MessageFormatter formatter = mockMessageFormatter();
    String value = "Now the student has become the master.";
    replay();
    MaxLength validator = new MaxLength(null);
    validator.validate(field, value.length(), formatter, value);
    verify();
}
Also used : Field(org.apache.tapestry5.Field) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)32 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)26 FieldValidator (org.apache.tapestry5.FieldValidator)21 Field (org.apache.tapestry5.Field)16 Validator (org.apache.tapestry5.Validator)16 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)13 ComponentResources (org.apache.tapestry5.ComponentResources)12 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)12 FormSupport (org.apache.tapestry5.services.FormSupport)12 Messages (org.apache.tapestry5.commons.Messages)11 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)11 ValidationException (org.apache.tapestry5.ValidationException)7 Pattern (java.util.regex.Pattern)2 Validator (javax.validation.Validator)2 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)2 Request (org.apache.tapestry5.http.services.Request)2 BeanValidationContext (org.apache.tapestry5.internal.BeanValidationContext)2 Html5Support (org.apache.tapestry5.services.Html5Support)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1