Search in sources :

Example 21 with ValidatorContext

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

the class ShortRangeFieldValidatorTest method testMinValidation.

public void testMinValidation() throws Exception {
    // given
    ValidationAction action = prepareAction((short) 1);
    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 1", 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 22 with ValidatorContext

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

the class BeanValidationInterceptor method addBeanValidationErrors.

@SuppressWarnings("nls")
protected void addBeanValidationErrors(Set<ConstraintViolation<Object>> constraintViolations, Object action) {
    if (constraintViolations != null) {
        ValidatorContext validatorContext = new DelegatingValidatorContext(action, textProviderFactory);
        for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
            String key = constraintViolation.getMessage();
            String message = key;
            try {
                message = validatorContext.getText(key);
                if (convertToUtf8 && StringUtils.isNotBlank(message)) {
                    message = new String(message.getBytes(convertFromEncoding), "UTF-8");
                }
            } catch (Exception e) {
                LOG.error("Error while trying to fetch message: {}", key, e);
            }
            if (isActionError(constraintViolation)) {
                LOG.debug("Adding action error [{}]", message);
                validatorContext.addActionError(message);
            } else {
                ValidationError validationError = buildBeanValidationError(constraintViolation, message);
                String fieldName = validationError.getFieldName();
                if (action instanceof ModelDriven && fieldName.startsWith(ValidatorConstants.MODELDRIVEN_PREFIX)) {
                    fieldName = fieldName.replace("model.", ValidatorConstants.EMPTY_SPACE);
                }
                LOG.debug("Adding field error [{}] with message [{}]", fieldName, validationError.getMessage());
                validatorContext.addFieldError(fieldName, validationError.getMessage());
            }
        }
    }
}
Also used : DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) ModelDriven(com.opensymphony.xwork2.ModelDriven)

Example 23 with ValidatorContext

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

the class AnnotationActionValidatorManagerTest method testSkipAllActionLevelShortCircuit2.

public void testSkipAllActionLevelShortCircuit2() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    try {
        AnnotationUser user = new AnnotationUser();
        user.setName("Mark");
        // * mark both email to starts with mark to get pass the action-level validator,
        // so we could concentrate on testing the field-level validators (AnnotationUser-validation.xml)
        // * make both email the same to pass the action-level validator at
        // AnnotationUserMarker-validation.xml
        user.setEmail("mark_bad_email_for_field_val@foo.com");
        user.setEmail2("mark_bad_email_for_field_val@foo.com");
        ValidatorContext context = new DummyValidatorContext(user, tpf);
        annotationActionValidatorManager.validate(user, null, context);
        assertTrue(context.hasFieldErrors());
        // check field errors
        // we have an error in this field level, email does not ends with mycompany.com
        List l = context.getFieldErrors().get("email");
        assertNotNull(l);
        // because email-field-val is short-circuit
        assertEquals(1, l.size());
        assertEquals("Email not from the right company.", l.get(0));
        // check action errors
        l = (List) context.getActionErrors();
        assertFalse(context.hasActionErrors());
        assertEquals(0, l.size());
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 24 with ValidatorContext

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

the class AppendingValidatorContextTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    action = container.inject(VisitorValidatorTestAction.class);
    TextProviderFactory tpf = container.getInstance(TextProviderFactory.class);
    ValidatorContext vc1 = new DelegatingValidatorContext(action, tpf);
    VisitorFieldValidator.AppendingValidatorContext vc2 = new AppendingValidatorContext(vc1, createTextProvider(action, vc1), FIRST_NAME, "");
    validatorContext = new AppendingValidatorContext(vc2, createTextProvider(action, vc2), SECOND_NAME, "");
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) VisitorValidatorTestAction(com.opensymphony.xwork2.validator.VisitorValidatorTestAction) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) AppendingValidatorContext(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) AppendingValidatorContext(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext) AppendingValidatorContext(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext)

Example 25 with ValidatorContext

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

the class DateRangeFieldValidatorTest method prepareValidator.

private DateRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
    ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
    valueStack.getActionContext().withLocale(new Locale("de"));
    valueStack.push(action);
    DateRangeFieldValidator validator = new DateRangeFieldValidator();
    validator.setValueStack(valueStack);
    validator.setMaxExpression("${dateMaxValue}");
    validator.setMinExpression("${dateMinValue}");
    validator.setValidatorContext(context);
    validator.setFieldName("dateRange");
    validator.setDefaultMessage("Max is ${dateMaxValue}, min is ${dateMinValue} but value is ${dateRange}");
    return validator;
}
Also used : Locale(java.util.Locale) ValueStack(com.opensymphony.xwork2.util.ValueStack) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory)

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