Search in sources :

Example 76 with Validator

use of com.opensymphony.xwork2.validator.Validator 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 77 with Validator

use of com.opensymphony.xwork2.validator.Validator 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 78 with Validator

use of com.opensymphony.xwork2.validator.Validator 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 79 with Validator

use of com.opensymphony.xwork2.validator.Validator 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

ValueStack (com.opensymphony.xwork2.util.ValueStack)19 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)18 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)18 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)16 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)12 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)9 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)7 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)6 HashMap (java.util.HashMap)6 ActionSupport (com.opensymphony.xwork2.ActionSupport)5 Validator (com.opensymphony.xwork2.validator.Validator)5 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)5 VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)4 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)3 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)3 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)3