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();
}
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));
}
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));
}
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);
}
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);
}
Aggregations