Search in sources :

Example 1 with DelegatingValidatorContext

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

the class VisitorFieldValidator method validateObject.

private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(o);
    ValidatorContext validatorContext;
    if (appendPrefix) {
        ValidatorContext parent = getValidatorContext();
        validatorContext = new AppendingValidatorContext(parent, createTextProvider(o, parent), fieldName, getMessage(o));
    } else {
        ValidatorContext parent = getValidatorContext();
        CompositeTextProvider textProvider = createTextProvider(o, parent);
        validatorContext = new DelegatingValidatorContext(parent, textProvider, parent);
    }
    actionValidatorManager.validate(o, visitorContext, validatorContext);
    stack.pop();
}
Also used : DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValueStack(com.opensymphony.xwork2.util.ValueStack) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) CompositeTextProvider(com.opensymphony.xwork2.CompositeTextProvider)

Example 2 with DelegatingValidatorContext

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

the class RepopulateConversionErrorFieldValidatorSupportTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    ValueStack stack = ActionContext.getContext().getValueStack();
    MockActionInvocation invocation = new MockActionInvocation();
    invocation.setStack(stack);
    ActionContext.getContext().withActionInvocation(invocation);
    String[] conversionErrorValue = new String[] { "some value" };
    Map<String, ConversionData> conversionErrors = ActionContext.getContext().getConversionErrors();
    conversionErrors.put("someFieldName", new ConversionData(conversionErrorValue, Integer.class));
    conversionErrors.put("xxxsomeFieldName", new ConversionData(conversionErrorValue, Integer.class));
    TextProviderFactory tpf = container.getInstance(TextProviderFactory.class);
    action = container.inject(ActionSupport.class);
    validator1 = new InternalRepopulateConversionErrorFieldValidatorSupport();
    validator1.setFieldName("someFieldName");
    validator1.setValidatorContext(new DelegatingValidatorContext(action, tpf));
    validator2 = new InternalRepopulateConversionErrorFieldValidatorSupport();
    validator2.setFieldName("someFieldName");
    validator2.setValidatorContext(new DelegatingValidatorContext(action, tpf) {

        @Override
        public String getFullFieldName(String fieldName) {
            return "xxx" + fieldName;
        }
    });
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) ValueStack(com.opensymphony.xwork2.util.ValueStack) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 3 with DelegatingValidatorContext

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

the class SimpleActionValidationTest method testMessageKeyIsReturnedIfNoOtherDefault.

public void testMessageKeyIsReturnedIfNoOtherDefault() throws ValidationException {
    Validator validator = new ValidatorSupport() {

        public void validate(Object object) throws ValidationException {
            addActionError(object);
        }
    };
    validator.setValueStack(ActionContext.getContext().getValueStack());
    String messageKey = "does.not.exist";
    validator.setMessageKey(messageKey);
    SimpleAction action = new SimpleAction();
    container.inject(action);
    ValidatorContext validatorContext = new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class));
    validator.setValidatorContext(validatorContext);
    validator.validate(this);
    assertTrue(validatorContext.hasActionErrors());
    Collection<String> errors = validatorContext.getActionErrors();
    assertEquals(1, errors.size());
    assertEquals(messageKey, errors.toArray()[0]);
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) SimpleAction(com.opensymphony.xwork2.SimpleAction) ValidatorSupport(com.opensymphony.xwork2.validator.validators.ValidatorSupport)

Example 4 with DelegatingValidatorContext

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

the class StringLengthFieldValidatorTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    action = new InternalActionSupport();
    container.inject(action);
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    valueStack.push(action);
    validator = new StringLengthFieldValidator();
    validator.setFieldName("myField");
    validator.setMessageKey("error");
    validator.setValidatorContext(new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class)));
    validator.setMaxLength(5);
    validator.setMinLength(2);
    validator.setValueStack(valueStack);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)

Example 5 with DelegatingValidatorContext

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

the class StringValidatorTest method testRequiredStringWithNullValue.

public void testRequiredStringWithNullValue() throws Exception {
    Equidae equidae = new Equidae();
    equidae.setHorse(null);
    DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
    assertTrue(context.hasFieldErrors());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) Equidae(com.opensymphony.xwork2.test.Equidae)

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