Search in sources :

Example 6 with ValidationAwareSupport

use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.

the class DWRValidator method doPost.

public ValidationAwareSupport doPost(String namespace, String actionName, Map params) throws Exception {
    HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
    ServletContext servletContext = WebContextFactory.get().getServletContext();
    HttpServletResponse res = WebContextFactory.get().getHttpServletResponse();
    HttpParameters.Builder requestParams = HttpParameters.create(req.getParameterMap());
    if (params != null) {
        requestParams = requestParams.withExtraParams(params);
    }
    Map<String, Object> requestMap = new RequestMap(req);
    Map<String, Object> session = new SessionMap<>(req);
    Map<String, Object> application = new ApplicationMap(servletContext);
    Dispatcher du = Dispatcher.getInstance();
    Map<String, Object> ctx = du.createContextMap(requestMap, requestParams.build(), session, application, req, res);
    try {
        ActionProxyFactory actionProxyFactory = du.getContainer().getInstance(ActionProxyFactory.class);
        ActionProxy proxy = actionProxyFactory.createActionProxy(namespace, actionName, null, ctx, true, true);
        proxy.execute();
        Object action = proxy.getAction();
        if (action instanceof ValidationAware) {
            ValidationAware aware = (ValidationAware) action;
            ValidationAwareSupport vas = new ValidationAwareSupport();
            vas.setActionErrors(aware.getActionErrors());
            vas.setActionMessages(aware.getActionMessages());
            vas.setFieldErrors(aware.getFieldErrors());
            return vas;
        } else {
            return null;
        }
    } catch (Exception e) {
        LOG.error("Error while trying to validate", e);
        return null;
    }
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ApplicationMap(org.apache.struts2.dispatcher.ApplicationMap) ActionProxy(com.opensymphony.xwork2.ActionProxy) HttpParameters(org.apache.struts2.dispatcher.HttpParameters) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMap(org.apache.struts2.dispatcher.RequestMap) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory) ServletContext(javax.servlet.ServletContext) SessionMap(org.apache.struts2.dispatcher.SessionMap) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 7 with ValidationAwareSupport

use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.

the class DoubleRangeFieldValidatorTest method testEdgeOfMaxRange.

public void testEdgeOfMaxRange() throws Exception {
    MyTestProduct prod = new MyTestProduct();
    prod.setName("coca cola");
    prod.setPrice(9.95);
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(prod);
    val.setFieldName("price");
    DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    val.setValidatorContext(context);
    val.setMaxInclusive(9.95d);
    // should pass
    val.validate(prod);
    assertFalse(context.hasErrors());
    assertEquals(9.95d, val.getMaxInclusive());
    val.setMaxExclusive(9.95d);
    // should not pass
    val.validate(prod);
    assertTrue(context.hasErrors());
    assertEquals(9.95d, val.getMaxExclusive());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 8 with ValidationAwareSupport

use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.

the class DoubleRangeFieldValidatorTest method testRangeNotADoubleObjectValueInStack.

public void testRangeNotADoubleObjectValueInStack() throws Exception {
    MyTestProduct prod = new MyTestProduct();
    prod.setName("coca cola");
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(prod);
    val.setMinInclusive(0d);
    val.setMaxInclusive(10d);
    val.setFieldName("name");
    DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    val.setValidatorContext(context);
    val.validate(prod);
    assertEquals(0d, val.getMinInclusive());
    assertEquals(10d, val.getMaxInclusive());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 9 with ValidationAwareSupport

use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.

the class DoubleRangeFieldValidatorTest method testEdgeOfMinRange.

public void testEdgeOfMinRange() throws Exception {
    MyTestProduct prod = new MyTestProduct();
    prod.setName("coca cola");
    prod.setPrice(9.95);
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(prod);
    val.setFieldName("price");
    DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    val.setValidatorContext(context);
    val.setMinInclusive(9.95d);
    // should pass
    val.validate(prod);
    assertFalse(context.hasErrors());
    val.setMinExclusive(9.95d);
    // should not pass
    val.validate(prod);
    assertTrue(context.hasErrors());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 10 with ValidationAwareSupport

use of com.opensymphony.xwork2.ValidationAwareSupport 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)

Aggregations

ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)15 ValueStack (com.opensymphony.xwork2.util.ValueStack)4 List (java.util.List)4 Equidae (com.opensymphony.xwork2.test.Equidae)3 Map (java.util.Map)2 ActionProxy (com.opensymphony.xwork2.ActionProxy)1 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)1 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)1 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)1 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 File (java.io.File)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ApplicationMap (org.apache.struts2.dispatcher.ApplicationMap)1 Dispatcher (org.apache.struts2.dispatcher.Dispatcher)1