You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Josep García <jg...@isigma.es> on 2011/11/16 18:19:59 UTC

using xwork validator programatically?

Is there any documentation on how to call xwork validation programatically?

The context: I have a dynamic form, which is a freemarker template, which
can have any fields that are then saved as xml in my Struts action.
I would like to have the possibility to call validation with a specific
*-validation.xml, defined at runtime, associated to the freemarker
template, so that I can apply xml defined validators against my action
context value stack. Is that possible?

Cheers,
Josep

Re: using xwork validator programatically?

Posted by Josep García <jg...@isigma.es>.
Regarding:


2011/12/1 Josep García <jg...@isigma.es>

>
> In order to validate dynamic form input fields (based on freemarker's
> ability to render input fields from xml tree), I've also had to code my own
> field validator, because my action does not have getter/setter for the
> dynamic fields.
>
> In my derived XmlRequiredFieldValidator, I reimplement getFieldValue:
>
>     protected Object getFieldValue(String name, Object object) throws
> ValidationException {
>         ValidateableXml action = (ValidateableXml) object;
>         Map<String, String[]> params = action.getParameterMap();
>         String[] val = params.get(name);
>         String str = null;
>         if (val != null) {
>             str = val[0];
>             if (str.isEmpty()) {
>                 str = null;
>             }
>         }
>         return str;
>     }
>
> I fear I'll probably have to rewrite xml*fieldvalidator for other
> validator types.
>
> If you can think of a better alternative, let me know.
>

The validation requires getters/setters on the action, and does not pickup
values from submitted input form params. Am I right?
Maybe, the ValueStack should also contain submitted params.

Cheers,
Josep

Re: using xwork validator programatically?

Posted by Josep García <jg...@isigma.es>.
JIRA created:
https://issues.apache.org/jira/browse/WW-3715



2011/12/1 Łukasz Lenart <lu...@googlemail.com>

> 2011/12/1 Josep García <jg...@isigma.es>
>
> > Basically,  validatorKey build from clazz AND context.
> >
> >         final String validatorKey = buildValidatorKey(clazz, context);
> >
> > I enclose a patch, with changes relative to xwork-core-2.1.6.
> >
> >
> > In order to validate dynamic form input fields (based on freemarker's
> > ability to render input fields from xml tree), I've also had to code my
> own
> > field validator, because my action does not have getter/setter for the
> > dynamic fields.
> >
> > In my derived XmlRequiredFieldValidator, I reimplement getFieldValue:
> >
> >     protected Object getFieldValue(String name, Object object) throws
> > ValidationException {
> >         ValidateableXml action = (ValidateableXml) object;
> >         Map<String, String[]> params = action.getParameterMap();
> >         String[] val = params.get(name);
> >         String str = null;
> >         if (val != null) {
> >             str = val[0];
> >             if (str.isEmpty()) {
> >                 str = null;
> >             }
> >         }
> >         return str;
> >     }
>
>
>
> Could you register an issue [1] and attach the patch there with granting
> rights to ASF ?
>
> [1] https://issues.apache.org/jira/browse/WW
>
>
> Thanks in advance
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>

Re: using xwork validator programatically?

Posted by Łukasz Lenart <lu...@googlemail.com>.
2011/12/1 Josep García <jg...@isigma.es>

> Basically,  validatorKey build from clazz AND context.
>
>         final String validatorKey = buildValidatorKey(clazz, context);
>
> I enclose a patch, with changes relative to xwork-core-2.1.6.
>
>
> In order to validate dynamic form input fields (based on freemarker's
> ability to render input fields from xml tree), I've also had to code my own
> field validator, because my action does not have getter/setter for the
> dynamic fields.
>
> In my derived XmlRequiredFieldValidator, I reimplement getFieldValue:
>
>     protected Object getFieldValue(String name, Object object) throws
> ValidationException {
>         ValidateableXml action = (ValidateableXml) object;
>         Map<String, String[]> params = action.getParameterMap();
>         String[] val = params.get(name);
>         String str = null;
>         if (val != null) {
>             str = val[0];
>             if (str.isEmpty()) {
>                 str = null;
>             }
>         }
>         return str;
>     }



Could you register an issue [1] and attach the patch there with granting
rights to ASF ?

[1] https://issues.apache.org/jira/browse/WW


Thanks in advance
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

Re: using xwork validator programatically?

Posted by Josep García <jg...@isigma.es>.
Basically,  validatorKey build from clazz AND context.

        final String validatorKey = buildValidatorKey(clazz, context);

I enclose a patch, with changes relative to xwork-core-2.1.6.


In order to validate dynamic form input fields (based on freemarker's
ability to render input fields from xml tree), I've also had to code my own
field validator, because my action does not have getter/setter for the
dynamic fields.

In my derived XmlRequiredFieldValidator, I reimplement getFieldValue:

    protected Object getFieldValue(String name, Object object) throws
ValidationException {
        ValidateableXml action = (ValidateableXml) object;
        Map<String, String[]> params = action.getParameterMap();
        String[] val = params.get(name);
        String str = null;
        if (val != null) {
            str = val[0];
            if (str.isEmpty()) {
                str = null;
            }
        }
        return str;
    }

I fear I'll probably have to rewrite xml*fieldvalidator for other validator
types.

If you can think of a better alternative, let me know.

Cheers,
Josep

2011/11/30 Łukasz Lenart <lu...@googlemail.com>

> 2011/11/30 Josep García <jg...@isigma.es>
>
> > This way, it works, but the downside: 149 line source file to work around
> > the problem.
> >
>
> Could you prepare a patch and label it with 3.x ? Maybe we'll be able to
> refactor the manager with your changes
>
>
> Kind regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>

Re: using xwork validator programatically?

Posted by Łukasz Lenart <lu...@googlemail.com>.
2011/11/30 Josep García <jg...@isigma.es>

> This way, it works, but the downside: 149 line source file to work around
> the problem.
>

Could you prepare a patch and label it with 3.x ? Maybe we'll be able to
refactor the manager with your changes


Kind regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

Re: using xwork validator programatically?

Posted by Łukasz Lenart <lu...@googlemail.com>.
2011/11/30 Josep García <jg...@isigma.es>:
> If I pass in a dynamic context name, like in validatorManager.validate(this,
> "my-validation"), the AnnotationActionValidator calls its method
> getValidators:
>
>     public synchronized List<Validator> getValidators(Class clazz, String
> context, String method) {
>         final String validatorKey = buildValidatorKey(clazz);
>
>         if (validatorCache.containsKey(validatorKey)) {
>             if (FileManager.isReloadingConfigs()) {
>                 validatorCache.put(validatorKey,
> buildValidatorConfigs(clazz, context, true, null));
>             }
>         } else {
>             validatorCache.put(validatorKey, buildValidatorConfigs(clazz,
> context, false, null));
>         }
>
>         // get the set of validator configs
>         List<ValidatorConfig> cfgs = validatorCache.get(validatorKey);
>
> and if reload is false, it will not find the file
> MyAction-my-validation-validation.xml: the validatorKey is based on the
> clazz name, and not on the context.
> :-(

Hm... it should work, did you try to debug ?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: using xwork validator programatically?

Posted by Josep García <jg...@isigma.es>.
If I pass in a dynamic context name, like in
validatorManager.validate(this, "my-validation"), the
AnnotationActionValidator calls its method getValidators:

    public synchronized List<Validator> getValidators(Class clazz, String
context, String method) {
        final String validatorKey = buildValidatorKey(clazz);

        if (validatorCache.containsKey(validatorKey)) {
            if (FileManager.isReloadingConfigs()) {
                validatorCache.put(validatorKey,
buildValidatorConfigs(clazz, context, true, null));
            }
        } else {
            validatorCache.put(validatorKey, buildValidatorConfigs(clazz,
context, false, null));
        }

        // get the set of validator configs
        List<ValidatorConfig> cfgs = validatorCache.get(validatorKey);

and if reload is false, it will not find the file
MyAction-my-validation-validation.xml: the validatorKey is based on the
clazz name, and not on the context.
:-(

Any suggestion about this?
J.

2011/11/28 Łukasz Lenart <lu...@googlemail.com>

> 2011/11/28 Josep García <jg...@isigma.es>:
> > I have tried this option, not without a few workarounds: I had to add a
> > param to struts.xml so validation files are loaded during runtime:
> >     <constant name="struts.configuration.xml.reload" value="true"/>
>
> Hmmm.... that's bad, each time the configs will be reload from the
> disk and performance of the application will decrease :/
>
> > I have arrived to a leve in which I can call, from my Action:
> >
> >             validatorManager.validate(this, "my-validation");
> >
> > and this tries to validate the form against a MyAction-my-validation.xml
> > file with the validation rules.
> > So far, so good.
> > However, my form post the user input as form parameters of the type:
> > "dom.myparam1", "dom.myparam2", etc.
> > Within the validation process, the validator calls OgnlContext.get(Object
> > key) to retrieve the value to validate agains a validation rule, but
> fails
> > to retrieve it.
> > While debugging, I can see the params are there in a map entry: Key:
> > "com.opensymphony.xwork2.ActionContext.parameters", and in value: all my
> > form params.
> > buut it fails to get the value.
> >
> > Any possible workaround for this? Can I make OgnlContext look into the
> > ActionContext.parameters map? Or should I add programatically my dynamic
> > fields to the Ognl Context? Is that possible? How?
>
> OGNL should search through out the stack, maybe try to put those
> parameters on top ?
>
>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: using xwork validator programatically?

Posted by Łukasz Lenart <lu...@googlemail.com>.
2011/11/28 Josep García <jg...@isigma.es>:
> I have tried this option, not without a few workarounds: I had to add a
> param to struts.xml so validation files are loaded during runtime:
>     <constant name="struts.configuration.xml.reload" value="true"/>

Hmmm.... that's bad, each time the configs will be reload from the
disk and performance of the application will decrease :/

> I have arrived to a leve in which I can call, from my Action:
>
>             validatorManager.validate(this, "my-validation");
>
> and this tries to validate the form against a MyAction-my-validation.xml
> file with the validation rules.
> So far, so good.
> However, my form post the user input as form parameters of the type:
> "dom.myparam1", "dom.myparam2", etc.
> Within the validation process, the validator calls OgnlContext.get(Object
> key) to retrieve the value to validate agains a validation rule, but fails
> to retrieve it.
> While debugging, I can see the params are there in a map entry: Key:
> "com.opensymphony.xwork2.ActionContext.parameters", and in value: all my
> form params.
> buut it fails to get the value.
>
> Any possible workaround for this? Can I make OgnlContext look into the
> ActionContext.parameters map? Or should I add programatically my dynamic
> fields to the Ognl Context? Is that possible? How?

OGNL should search through out the stack, maybe try to put those
parameters on top ?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: using xwork validator programatically?

Posted by Josep García <jg...@isigma.es>.
Thanks for the tip, Łukasz.

I have tried this option, not without a few workarounds: I had to add a
param to struts.xml so validation files are loaded during runtime:
    <constant name="struts.configuration.xml.reload" value="true"/>


I have arrived to a leve in which I can call, from my Action:

            validatorManager.validate(this, "my-validation");

and this tries to validate the form against a MyAction-my-validation.xml
file with the validation rules.
So far, so good.
However, my form post the user input as form parameters of the type:
"dom.myparam1", "dom.myparam2", etc.
Within the validation process, the validator calls OgnlContext.get(Object
key) to retrieve the value to validate agains a validation rule, but fails
to retrieve it.
While debugging, I can see the params are there in a map entry: Key:
"com.opensymphony.xwork2.ActionContext.parameters", and in value: all my
form params.
buut it fails to get the value.

Any possible workaround for this? Can I make OgnlContext look into the
ActionContext.parameters map? Or should I add programatically my dynamic
fields to the Ognl Context? Is that possible? How?

Cheers,
Josep

2011/11/18 Łukasz Lenart <lu...@googlemail.com>

> You can try to obtain ActionValidatorManager instance (@Inject in to
> an action) and try to use one of its methods.
>
>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>
>
> 2011/11/16 Josep García <jg...@isigma.es>:
> > Is there any documentation on how to call xwork validation
> programatically?
> >
> > The context: I have a dynamic form, which is a freemarker template, which
> > can have any fields that are then saved as xml in my Struts action.
> > I would like to have the possibility to call validation with a specific
> > *-validation.xml, defined at runtime, associated to the freemarker
> > template, so that I can apply xml defined validators against my action
> > context value stack. Is that possible?
> >
> > Cheers,
> > Josep
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: using xwork validator programatically?

Posted by Łukasz Lenart <lu...@googlemail.com>.
You can try to obtain ActionValidatorManager instance (@Inject in to
an action) and try to use one of its methods.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/


2011/11/16 Josep García <jg...@isigma.es>:
> Is there any documentation on how to call xwork validation programatically?
>
> The context: I have a dynamic form, which is a freemarker template, which
> can have any fields that are then saved as xml in my Struts action.
> I would like to have the possibility to call validation with a specific
> *-validation.xml, defined at runtime, associated to the freemarker
> template, so that I can apply xml defined validators against my action
> context value stack. Is that possible?
>
> Cheers,
> Josep
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org