use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.
the class BeanFieldValidator method validate.
@Override
@SuppressWarnings("unchecked")
public void validate(final Object value) throws ValidationException {
final BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
if (beanValidationContext == null) {
return;
}
final Validator validator = validatorFactory.getValidator();
String currentProperty = beanValidationContext.getCurrentProperty();
if (currentProperty == null)
return;
final ValidationInfo validationInfo = getValidationInfo(beanValidationContext, currentProperty, validator);
final PropertyDescriptor propertyDescriptor = validationInfo.getPropertyDescriptor();
if (propertyDescriptor == null)
return;
final Set<ConstraintViolation<Object>> violations = validator.validateValue((Class<Object>) validationInfo.getBeanType(), validationInfo.getPropertyName(), value, beanValidationGroupSource.get());
if (violations.isEmpty()) {
return;
}
final StringBuilder builder = new StringBuilder();
for (Iterator<ConstraintViolation<Object>> iterator = violations.iterator(); iterator.hasNext(); ) {
ConstraintViolation<?> violation = iterator.next();
builder.append(format("%s %s", field.getLabel(), violation.getMessage()));
if (iterator.hasNext())
builder.append(", ");
}
throw new ValidationException(builder.toString());
}
use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.
the class TapestryModule method contributeBindingSource.
// ========================================================================
//
// Service Builder Methods (static)
//
// ========================================================================
// ========================================================================
//
// Service Contribution Methods (static)
//
// ========================================================================
/**
* Contributes the factory for several built-in binding prefixes ("asset",
* "block", "component", "literal", prop",
* "nullfieldstrategy", "message", "validate", "translate", "var").
*/
public static void contributeBindingSource(MappedConfiguration<String, BindingFactory> configuration, @InjectService("PropBindingFactory") BindingFactory propBindingFactory, @InjectService("MessageBindingFactory") BindingFactory messageBindingFactory, @InjectService("ValidateBindingFactory") BindingFactory validateBindingFactory, @InjectService("TranslateBindingFactory") BindingFactory translateBindingFactory, @InjectService("AssetBindingFactory") BindingFactory assetBindingFactory, @InjectService("NullFieldStrategyBindingFactory") BindingFactory nullFieldStrategyBindingFactory, @InjectService("ContextBindingFactory") BindingFactory contextBindingFactory, @InjectService("SymbolBindingFactory") BindingFactory symbolBindingFactory) {
configuration.add(BindingConstants.LITERAL, new LiteralBindingFactory());
configuration.add(BindingConstants.COMPONENT, new ComponentBindingFactory());
configuration.add(BindingConstants.VAR, new RenderVariableBindingFactory());
configuration.add(BindingConstants.BLOCK, new BlockBindingFactory());
configuration.add(BindingConstants.PROP, propBindingFactory);
configuration.add(BindingConstants.MESSAGE, messageBindingFactory);
configuration.add(BindingConstants.VALIDATE, validateBindingFactory);
configuration.add(BindingConstants.TRANSLATE, translateBindingFactory);
configuration.add(BindingConstants.ASSET, assetBindingFactory);
configuration.add(BindingConstants.NULLFIELDSTRATEGY, nullFieldStrategyBindingFactory);
configuration.add(BindingConstants.CONTEXT, contextBindingFactory);
configuration.add(BindingConstants.SYMBOL, symbolBindingFactory);
}
use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.
the class TapestryModule method contributeValidationConstraintGenerator.
/**
* Adds built-in constraint generators:
* <ul>
* <li>PrimtiveField -- primitive fields are always required
* <li>ValidateAnnotation -- adds constraints from a {@link Validate} annotation
* </ul>
*/
public static void contributeValidationConstraintGenerator(OrderedConfiguration<ValidationConstraintGenerator> configuration) {
configuration.add("PrimitiveField", new PrimitiveFieldConstraintGenerator());
configuration.add("ValidateAnnotation", new ValidateAnnotationConstraintGenerator());
configuration.addInstance("Messages", MessagesConstraintGenerator.class);
}
use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.
the class RequiredTest method null_value.
@Test
public void null_value() {
Field field = mockFieldWithLabel("My Field");
MessageFormatter formatter = mockMessageFormatter();
train_format(formatter, "{message}", "My Field");
Html5Support html5Support = mockHtml5Support();
replay();
try {
new Required(null, html5Support).validate(field, null, formatter, null);
unreachable();
} catch (ValidationException ex) {
assertEquals(ex.getMessage(), "{message}");
}
verify();
}
Aggregations