[ 
https://issues.apache.org/struts/browse/STR-2615?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul Benedict closed STR-2615.
------------------------------

    Resolution: Won't Fix
      Assignee:     (was: Struts Developers)

Closing this issue because it is not solved by enhancing Struts. Please make a 
ticket in Commons Validator if you wish to use scripting in XML.

> if-Variable for any Validator rule
> ----------------------------------
>
>                 Key: STR-2615
>                 URL: https://issues.apache.org/struts/browse/STR-2615
>             Project: Struts 1
>          Issue Type: Improvement
>          Components: Core
>    Affects Versions: 1.2.7
>         Environment: Operating System: All
> Platform: All
>            Reporter: Thomas Jacob
>            Priority: Minor
>
> I'm am proposing an extension to the Validator of Struts.
> It adds the feature to declare a variable 'if' in the validation.xml.
> The value of this variable is an EL expression.
> If and only if this EL expression evaluates to true,
> the field's rules are considered.
> The EL expression may contain a number of variables,
> including the current form bean, the property's value,
> and the request, session, and application scopes.
> Using EL, and providing this context, enables a much more
> flexible approach to switching rules on and off dynamically.
> Currently, I am extending the FieldChecks class, calling a
> switching method in each rule's method. A nicer integration
> of my source code would be very much appreciated.
> The implementation uses Commons EL.
> People told be that this contribution is generic enough
> to be part of Struts. Perhaps you get to the same conclusion :-)
> This is the code snippet to switch:
> protected static boolean skipValidation(final Object bean, final 
> ValidatorAction 
> action, final Field field,
>       final HttpServletRequest request)
> {
>       Var ifVariable = field.getVar("if");
>       if (ifVariable == null)
>       {
>               return false;
>       }
>       String ifExpression = ifVariable.getValue();
>       ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl();
>       VariableResolver resolver = new VariableResolver()
>       {
>               public Object resolveVariable(String name) throws ELException
>               {
>                       HttpSession session = request.getSession();
>                       ServletContext context = session.getServletContext();
>                       if (name.equals("form"))
>                       {
>                               return bean;
>                       }
>                       else if (name.equals("requestScope"))
>                       {
>                               Map requestMap = new 
> HashMap(request.getParameterMap());
>                               Enumeration enumeration = 
> request.getAttributeNames();
>                               while (enumeration.hasMoreElements())
>                               {
>                                       String attributeName = (String) 
> enumeration.
> nextElement();
>                                       Object attributeValue = request.
> getAttribute(attributeName);
>                                       requestMap.put(attributeName, 
> attributeValue);
>                               }
>                               return requestMap;
>                       }
>                       else if (name.equals("sessionScope"))
>                       {
>                               Map sessionMap = new HashMap();
>                               Enumeration enumeration = 
> session.getAttributeNames();
>                               while (enumeration.hasMoreElements())
>                               {
>                                       String attributeName = (String) 
> enumeration.
> nextElement();
>                                       Object attributeValue = session.
> getAttribute(attributeName);
>                                       sessionMap.put(attributeName, 
> attributeValue);
>                               }
>                               return sessionMap;
>                       }
>                       else if (name.equals("applicationScope"))
>                       {
>                               Map contextMap = new HashMap();
>                               Enumeration enumeration = 
> context.getAttributeNames();
>                               while (enumeration.hasMoreElements())
>                               {
>                                       String attributeName = (String) 
> enumeration.
> nextElement();
>                                       Object attributeValue = context.
> getAttribute(attributeName);
>                                       contextMap.put(attributeName, 
> attributeValue);
>                               }
>                               return contextMap;
>                       }
>                       else if (name.equals("value"))
>                       {
>                               return ValidatorUtils.getValueAsString(bean, 
> field.
> getProperty());
>                       }
>                       else
>                       {
>                               Object bean = request.getAttribute(name);
>                               if (bean != null)
>                               {
>                                       return bean;
>                               }
>                               bean = session.getAttribute(name);
>                               if (bean != null)
>                               {
>                                       return bean;
>                               }
>                               return context.getAttribute(name);
>                       }
>               }
>       };
>       try
>       {
>               Boolean result = (Boolean) evaluator.evaluate("${" + ifVariable.
> getValue() + "}", Boolean.class, resolver,
>                       null);
>               return result != null && !result.booleanValue();
>       }
>       catch (ELException exception)
>       {
>               throw new IllegalArgumentException("Invalid validator 'if' EL 
> expression '" + ifExpression + "': " + exception);
>       }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to