Search in sources :

Example 16 with DelegatingValidatorContext

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

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

the class StringValidatorTest method testStringLength.

public void testStringLength() throws Exception {
    Equidae equidae = new Equidae();
    equidae.setCow("asdf");
    equidae.setDonkey("asdf");
    ActionContext.getContext().getValueStack().push(equidae);
    DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
    Map fieldErrors = context.getFieldErrors();
    // cow
    assertTrue(fieldErrors.containsKey("cow"));
    List errors = (List) fieldErrors.get("cow");
    assertEquals(2, errors.size());
    assertEquals("noTrim-min5", errors.get(0));
    assertEquals("noTrim-min5-max10", errors.get(1));
    // donkey
    assertTrue(fieldErrors.containsKey("donkey"));
    errors = (List) fieldErrors.get("donkey");
    assertEquals(2, errors.size());
    assertEquals("trim-min5", errors.get(0));
    assertEquals("trim-min5-max10", errors.get(1));
    equidae.setCow("asdf  ");
    equidae.setDonkey("asdf  ");
    ActionContext.getContext().getValueStack().push(equidae);
    context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
    fieldErrors = context.getFieldErrors();
    // cow
    assertFalse(fieldErrors.containsKey("cow"));
    // donkey
    assertTrue(fieldErrors.containsKey("donkey"));
    errors = (List) fieldErrors.get("donkey");
    assertEquals(2, errors.size());
    assertEquals("trim-min5", errors.get(0));
    assertEquals("trim-min5-max10", errors.get(1));
    equidae.setCow("asdfasdf");
    equidae.setDonkey("asdfasdf");
    ActionContext.getContext().getValueStack().push(equidae);
    context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
    fieldErrors = context.getFieldErrors();
    // cow
    assertFalse(fieldErrors.containsKey("cow"));
    // donkey
    assertFalse(fieldErrors.containsKey("donkey"));
    equidae.setCow("asdfasdf   ");
    equidae.setDonkey("asdfasdf   ");
    ActionContext.getContext().getValueStack().push(equidae);
    context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
    fieldErrors = context.getFieldErrors();
    // cow
    assertTrue(fieldErrors.containsKey("cow"));
    errors = (List) fieldErrors.get("cow");
    assertEquals(2, errors.size());
    assertEquals("noTrim-min5-max10", errors.get(0));
    assertEquals("noTrim-max10", errors.get(1));
    // donkey
    assertFalse(fieldErrors.containsKey("donkey"));
    equidae.setCow("asdfasdfasdf");
    equidae.setDonkey("asdfasdfasdf");
    ActionContext.getContext().getValueStack().push(equidae);
    context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
    fieldErrors = context.getFieldErrors();
    // cow
    assertTrue(fieldErrors.containsKey("cow"));
    errors = (List) fieldErrors.get("cow");
    assertEquals(2, errors.size());
    assertEquals("noTrim-min5-max10", errors.get(0));
    assertEquals("noTrim-max10", errors.get(1));
    // donkey
    assertTrue(fieldErrors.containsKey("donkey"));
    errors = (List) fieldErrors.get("donkey");
    assertEquals(2, errors.size());
    assertEquals("trim-min5-max10", errors.get(0));
    assertEquals("trim-max10", errors.get(1));
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) List(java.util.List) Equidae(com.opensymphony.xwork2.test.Equidae) Map(java.util.Map)

Example 18 with DelegatingValidatorContext

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

the class StringValidatorTest method testRequiredString.

public void testRequiredString() throws Exception {
    Equidae equidae = new Equidae();
    // everything should fail
    equidae.setHorse("");
    ActionContext.getContext().getValueStack().push(equidae);
    DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
    Map fieldErrors = context.getFieldErrors();
    assertTrue(fieldErrors.containsKey("horse"));
    assertEquals(2, ((List) fieldErrors.get("horse")).size());
    // trim = false should fail
    equidae.setHorse("  ");
    ActionContext.getContext().getValueStack().push(equidae);
    context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
    fieldErrors = context.getFieldErrors();
    assertTrue(fieldErrors.containsKey("horse"));
    List errors = (List) fieldErrors.get("horse");
    assertEquals(1, errors.size());
    assertEquals("trim", (String) errors.get(0));
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) List(java.util.List) Equidae(com.opensymphony.xwork2.test.Equidae) Map(java.util.Map)

Example 19 with DelegatingValidatorContext

use of com.opensymphony.xwork2.validator.DelegatingValidatorContext 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)9 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)4 DelegatingValidatorContext (com.opensymphony.xwork2.validator.DelegatingValidatorContext)4 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)4 Equidae (com.opensymphony.xwork2.test.Equidae)3 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)2 List (java.util.List)2 Map (java.util.Map)2 Mock (com.mockobjects.dynamic.Mock)1 CompositeTextProvider (com.opensymphony.xwork2.CompositeTextProvider)1 ModelDriven (com.opensymphony.xwork2.ModelDriven)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)1 VisitorValidatorTestAction (com.opensymphony.xwork2.validator.VisitorValidatorTestAction)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)1 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)1