Search in sources :

Example 26 with Validate

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());
}
Also used : ValidationException(org.apache.tapestry5.ValidationException) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) BeanValidationContext(org.apache.tapestry5.internal.BeanValidationContext) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) FieldValidator(org.apache.tapestry5.FieldValidator)

Example 27 with Validate

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);
}
Also used : LiteralBindingFactory(org.apache.tapestry5.internal.bindings.LiteralBindingFactory) BlockBindingFactory(org.apache.tapestry5.internal.bindings.BlockBindingFactory) RenderVariableBindingFactory(org.apache.tapestry5.internal.bindings.RenderVariableBindingFactory) ComponentBindingFactory(org.apache.tapestry5.internal.bindings.ComponentBindingFactory)

Example 28 with Validate

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);
}
Also used : ValidateAnnotationConstraintGenerator(org.apache.tapestry5.internal.beaneditor.ValidateAnnotationConstraintGenerator) PrimitiveFieldConstraintGenerator(org.apache.tapestry5.internal.beaneditor.PrimitiveFieldConstraintGenerator)

Example 29 with Validate

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

Aggregations

Test (org.testng.annotations.Test)16 Field (org.apache.tapestry5.Field)7 ValidationException (org.apache.tapestry5.ValidationException)5 Validate (org.apache.tapestry5.beaneditor.Validate)5 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)5 UploadedFile (org.apache.tapestry5.upload.services.UploadedFile)5 PropertyConduit (org.apache.tapestry5.beanmodel.PropertyConduit)4 Html5Support (org.apache.tapestry5.services.Html5Support)4 ValidationConstraintGenerator (org.apache.tapestry5.services.ValidationConstraintGenerator)4 Messages (org.apache.tapestry5.commons.Messages)3 MultipartDecoder (org.apache.tapestry5.upload.services.MultipartDecoder)3 FieldValidator (org.apache.tapestry5.FieldValidator)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 BeanValidationContextImpl (org.apache.tapestry5.internal.BeanValidationContextImpl)2 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)2 Heartbeat (org.apache.tapestry5.services.Heartbeat)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1