Search in sources :

Example 36 with Validator

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

the class LongRangeFieldValidatorTest method prepareValidator.

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

Example 37 with Validator

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

the class RequiredStringValidatorTest method testTrimAsExpression.

public void testTrimAsExpression() throws Exception {
    // given
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    ActionSupport action = new ActionSupport() {

        public boolean getTrimValue() {
            return false;
        }
    };
    valueStack.push(action);
    RequiredStringValidator validator = new RequiredStringValidator();
    validator.setValueStack(valueStack);
    assertTrue(validator.isTrim());
    // when
    validator.setTrimExpression("${trimValue}");
    // then
    assertFalse(validator.isTrim());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 38 with Validator

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

the class RequiredStringValidatorTest method testRequiredArrayOfStringsPass.

public void testRequiredArrayOfStringsPass() throws Exception {
    // given
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    ValidationAction action = new ValidationAction();
    action.setStrings(new String[] { "", "12334", null });
    valueStack.push(action);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    RequiredStringValidator validator = new RequiredStringValidator();
    validator.setValidatorContext(context);
    validator.setFieldName("strings");
    validator.setValueStack(valueStack);
    // when
    validator.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertEquals(2, context.getFieldErrors().get("strings").size());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 39 with Validator

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

the class RequiredStringValidatorTest method testRequiredStringFails.

public void testRequiredStringFails() throws Exception {
    // given
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    ValidationAction action = new ValidationAction();
    valueStack.push(action);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    RequiredStringValidator validator = new RequiredStringValidator();
    validator.setValidatorContext(context);
    validator.setFieldName("stringValue");
    validator.setValueStack(valueStack);
    validator.setDefaultMessage("Field ${fieldName} is required");
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 1);
    assertEquals(context.getFieldErrors().get("stringValue").get(0), "Field stringValue is required");
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 40 with Validator

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

the class RequiredStringValidatorTest method testRequiredCollectionOfStringsPass.

public void testRequiredCollectionOfStringsPass() throws Exception {
    // given
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    ValidationAction action = new ValidationAction();
    action.setStringCollection(Arrays.asList("", "123456", null));
    valueStack.push(action);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    RequiredStringValidator validator = new RequiredStringValidator();
    validator.setValidatorContext(context);
    validator.setFieldName("stringCollection");
    validator.setValueStack(valueStack);
    // when
    validator.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertEquals(2, context.getFieldErrors().get("stringCollection").size());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

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