Search in sources :

Example 1 with MessageFormatter

use of org.apache.tapestry5.commons.MessageFormatter 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 2 with MessageFormatter

use of org.apache.tapestry5.commons.MessageFormatter 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 3 with MessageFormatter

use of org.apache.tapestry5.commons.MessageFormatter 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)

Example 4 with MessageFormatter

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

the class MaxTest method small_enough.

@Test
public void small_enough() throws Exception {
    Field field = mockField();
    MessageFormatter formatter = mockMessageFormatter();
    Long constraint = 50L;
    Max validator = new Max(null, mockHtml5Support());
    replay();
    for (int value = 48; value <= 50; value++) validator.validate(field, constraint, formatter, value);
    verify();
}
Also used : Field(org.apache.tapestry5.Field) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) Test(org.testng.annotations.Test)

Example 5 with MessageFormatter

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

the class MinLengthTest method short_value.

@Test
public void short_value() throws Exception {
    String label = "My Field";
    Field field = mockFieldWithLabel(label);
    MessageFormatter formatter = mockMessageFormatter();
    String value = "Now the student has become the master.";
    String message = "{message}";
    Integer constraint = value.length() + 1;
    train_format(formatter, message, constraint, label);
    replay();
    MinLength validator = new MinLength(null);
    try {
        validator.validate(field, constraint, formatter, value);
        unreachable();
    } catch (ValidationException ex) {
        assertEquals(ex.getMessage(), message);
    }
    verify();
}
Also used : Field(org.apache.tapestry5.Field) ValidationException(org.apache.tapestry5.ValidationException) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) Test(org.testng.annotations.Test)

Aggregations

MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)37 Test (org.testng.annotations.Test)35 Field (org.apache.tapestry5.Field)25 FieldValidator (org.apache.tapestry5.FieldValidator)14 Validator (org.apache.tapestry5.Validator)14 Messages (org.apache.tapestry5.commons.Messages)14 FormSupport (org.apache.tapestry5.services.FormSupport)14 ComponentResources (org.apache.tapestry5.ComponentResources)10 ValidationException (org.apache.tapestry5.ValidationException)9 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)9 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)9 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)9 Html5Support (org.apache.tapestry5.services.Html5Support)7 FieldTranslator (org.apache.tapestry5.FieldTranslator)5 MarkupWriter (org.apache.tapestry5.MarkupWriter)5 Translator (org.apache.tapestry5.Translator)5 FieldTranslatorSource (org.apache.tapestry5.services.FieldTranslatorSource)5 TranslatorSource (org.apache.tapestry5.services.TranslatorSource)5 Locale (java.util.Locale)4 Map (java.util.Map)4