Search in sources :

Example 31 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class ShortRangeFieldValidatorTest method prepareValidator.

private ShortRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
    ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
    valueStack.push(action);
    ShortRangeFieldValidator validator = new ShortRangeFieldValidator();
    validator.setValueStack(valueStack);
    validator.setMaxExpression("${shortMaxValue}");
    validator.setMinExpression("${shortMinValue}");
    validator.setValidatorContext(context);
    validator.setFieldName("shortRange");
    validator.setDefaultMessage("Max is ${shortMaxValue}, min is ${shortMinValue} but value is ${shortRange}");
    return validator;
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory)

Example 32 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class ShortRangeFieldValidatorTest method testMaxValidation.

public void testMaxValidation() throws Exception {
    // given
    ValidationAction action = prepareAction((short) 11);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    ShortRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 1);
    assertEquals("Max is 10, min is 2 but value is 11", context.getFieldErrors().get("shortRange").get(0));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 33 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class ShortRangeFieldValidatorTest method testPassValidation.

public void testPassValidation() throws Exception {
    // given
    ValidationAction action = prepareAction((short) 5);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    ShortRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 0);
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 34 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class MethodNameExtractor method addValidationErrors.

private void addValidationErrors(ConstraintViolation[] violations, Object action, ValueStack valueStack, String parentFieldname) {
    if (violations != null) {
        ValidatorContext validatorContext = new DelegatingValidatorContext(action, textProviderFactory);
        for (ConstraintViolation violation : violations) {
            // translate message
            String key = violation.getMessage();
            String message;
            // push context variable into stack, to allow use ${max}, ${min} etc in error messages
            valueStack.push(violation.getMessageVariables());
            // push the validator into the stack
            valueStack.push(violation.getContext());
            try {
                message = validatorContext.getText(key);
            } finally {
                valueStack.pop();
                valueStack.pop();
            }
            if (isActionError(violation)) {
                LOG.debug("Adding action error '{}'", message);
                validatorContext.addActionError(message);
            } else {
                ValidationError validationError = buildValidationError(violation.getContextPath(), message);
                // build field name
                String fieldName = validationError.getFieldName();
                if (parentFieldname != null) {
                    fieldName = parentFieldname + "." + fieldName;
                }
                LOG.debug("Adding field error [{}] with message '{}'", fieldName, validationError.getMessage());
                validatorContext.addFieldError(fieldName, validationError.getMessage());
                // add violations of member object fields
                addValidationErrors(violation.getCauses(), action, valueStack, fieldName);
            }
        }
    }
}
Also used : DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) ConstraintViolation(net.sf.oval.ConstraintViolation)

Aggregations

ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)21 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)17 ValueStack (com.opensymphony.xwork2.util.ValueStack)10 List (java.util.List)6 AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)4 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)4 DelegatingValidatorContext (com.opensymphony.xwork2.validator.DelegatingValidatorContext)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 AnnotatedTestBean (com.opensymphony.xwork2.AnnotatedTestBean)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ActionContext (com.opensymphony.xwork2.ActionContext)1 CompositeTextProvider (com.opensymphony.xwork2.CompositeTextProvider)1 ModelDriven (com.opensymphony.xwork2.ModelDriven)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)1 VisitorValidatorTestAction (com.opensymphony.xwork2.validator.VisitorValidatorTestAction)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 ValidatorSupport (com.opensymphony.xwork2.validator.validators.ValidatorSupport)1 AppendingValidatorContext (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext)1 HashMap (java.util.HashMap)1