Search in sources :

Example 41 with Validator

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

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

the class ValidatorSupportTest method testConditionalParseExpression.

public void testConditionalParseExpression() {
    OgnlValueStack stack = (OgnlValueStack) container.getInstance(ValueStackFactory.class).createValueStack();
    stack.getContext().put("something", "somevalue");
    ActionContext.of(stack.getContext()).withContainer(container).bind();
    ValidatorSupport validator = new ValidatorSupport() {

        public void validate(Object object) throws ValidationException {
        }
    };
    validator.setValueStack(ActionContext.getContext().getValueStack());
    String result1 = validator.parse("${#something}", String.class).toString();
    assertEquals(result1, "somevalue");
}
Also used : OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack)

Example 43 with Validator

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

the class PlexusObjectFactory method buildValidator.

/* (non-Javadoc)
     * @see com.opensymphony.xwork2.ObjectFactory#buildValidator(java.lang.String, java.util.Map, java.util.Map)
     */
public Validator buildValidator(String className, Map params, Map extraContext) throws Exception {
    Map context = new HashMap();
    context.put(PLEXUS_COMPONENT_TYPE, Validator.class.getName());
    Validator validator = (Validator) buildBean(className, context);
    reflectionProvider.setProperties(params, validator);
    return validator;
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Validator(com.opensymphony.xwork2.validator.Validator)

Example 44 with Validator

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

the class BeanValidationInterceptor method doIntercept.

@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
    Validator validator = this.beanValidationManager.getValidator();
    if (validator == null) {
        LOG.debug("There is no Bean Validator configured in class path. Skipping Bean validation..");
        return invocation.invoke();
    }
    LOG.debug("Starting bean validation using validator: {}", validator.getClass());
    Object action = invocation.getAction();
    ActionProxy actionProxy = invocation.getProxy();
    String methodName = actionProxy.getMethod();
    LOG.debug("Validating [{}/{}] with method [{}]", invocation.getProxy().getNamespace(), invocation.getProxy().getActionName(), methodName);
    if (null == MethodUtils.getAnnotation(getActionMethod(action.getClass(), methodName), SkipValidation.class, true, true)) {
        Class<?>[] validationGroup = getValidationGroups(action, methodName);
        // performing bean validation on action
        performBeanValidation(action, validator, validationGroup);
    }
    return invocation.invoke();
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) SkipValidation(org.apache.struts2.interceptor.validation.SkipValidation) Validator(javax.validation.Validator)

Example 45 with Validator

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

the class SpringObjectFactoryTest method testFallsBackToDefaultObjectFactoryValidatorBuilding.

public void testFallsBackToDefaultObjectFactoryValidatorBuilding() throws Exception {
    Map<String, Object> extraContext = new HashMap<>();
    Validator validator = objectFactory.buildValidator(RequiredStringValidator.class.getName(), new HashMap<String, Object>(), extraContext);
    assertEquals(RequiredStringValidator.class, validator.getClass());
}
Also used : HashMap(java.util.HashMap) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) Validator(com.opensymphony.xwork2.validator.Validator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator)

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