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