You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Quinn Stone <qs...@stoneconsultants.com> on 2005/08/26 18:38:03 UTC

Re-use field validations?

Is there any way to define the validation for a common form field once in
validation.xml and re-use the validation in other <form> blocks?

This would allow validation.xml to be updated in one place rather than many
places, if validation requirements change for a field that is used on many
forms. It would also help ensure that I (or others) won't accidentally
incorrectly code different validations for the same field on different forms.
The benefits will compound if I start writing custom validation routines.

I am new to Struts, and this is my first project using it, so I hope I'm not
missing something obvious.
 
Thanks,
Quinn


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


Re: Re-use field validations?

Posted by Don Brown <do...@gmail.com>.
I should also add that you cannot use external XML entities in validator XML 
files for any released version of Struts. However, Struts 1.3, as of 
tonight, will include code, dependent on Validator 1.2, which will allow 
their use.

Don

On 8/29/05, Hubert Rabago <hr...@gmail.com> wrote:
> 
> Fyi, Struts 1.3 is moving to a Validator 1.2 dependency. This version
> includes support for the form "extends" attribute which provides just
> what you were asking for.
> 
> Hubert
> 
> On 8/26/05, Laurie Harper <la...@holoweb.net> wrote:
> > Quinn Stone wrote:
> > > Is there any way to define the validation for a common form field once 
> in
> > > validation.xml and re-use the validation in other <form> blocks?
> > >
> > > This would allow validation.xml to be updated in one place rather than 
> many
> > > places, if validation requirements change for a field that is used on 
> many
> > > forms. It would also help ensure that I (or others) won't accidentally
> > > incorrectly code different validations for the same field on different 
> forms.
> > > The benefits will compound if I start writing custom validation 
> routines.
> > >
> > > I am new to Struts, and this is my first project using it, so I hope 
> I'm not
> > > missing something obvious.
> >
> > I guess what you're looking for is the ability to specify a list/set of
> > validations (e.g. 'depends="required,mask"') and associated parameters,
> > messages, etc. once for a field and have that same configuration applied 
> on
> > different forms using that field, right? Unfortunately, as far as I 
> know,
> > validator doesn't provide any facility to accomplish that directly.
> > However, I can think of one or two ways you could achieve it.
> >
> > The first would be using XML entities to setup and reference your 
> re-used
> > validations, something like:
> >
> > <DOCTYPE ... [
> > <!ENTITY field1 "<field name="loginid" depends="required"...>
> > ]>
> > ...
> > <form ...>
> > &field1;
> > ...
> > </form>
> >
> > (it may take some fiddling with syntax in the entity definition, and 
> using
> > external entities might be easier).
> >
> > The second would be to define custom validation rules that encapsulate 
> the
> > required composition and parameterization under a single name so that
> > everywhere the field is used you'd specify 
> 'depends="my-composite-validation"'.
> >
> > If there are any better ways to do this, I'd be interested to hear about
> > them too :-)
> >
> > L.
> > --
> > Laurie Harper
> > Open Source advocate, Java geek: http://www.holoweb.net/laurie
> > Founder, Zotech Software: http://www.zotechsoftware.com/
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

Re: Re-use field validations?

Posted by Hubert Rabago <hr...@gmail.com>.
Fyi, Struts 1.3 is moving to a Validator 1.2 dependency.  This version
includes support for the form "extends" attribute which provides just
what you were asking for.

Hubert

On 8/26/05, Laurie Harper <la...@holoweb.net> wrote:
> Quinn Stone wrote:
> > Is there any way to define the validation for a common form field once in
> > validation.xml and re-use the validation in other <form> blocks?
> >
> > This would allow validation.xml to be updated in one place rather than many
> > places, if validation requirements change for a field that is used on many
> > forms. It would also help ensure that I (or others) won't accidentally
> > incorrectly code different validations for the same field on different forms.
> > The benefits will compound if I start writing custom validation routines.
> >
> > I am new to Struts, and this is my first project using it, so I hope I'm not
> > missing something obvious.
> 
> I guess what you're looking for is the ability to specify a list/set of
> validations (e.g. 'depends="required,mask"') and associated parameters,
> messages, etc. once for a field and have that same configuration applied on
> different forms using that field, right? Unfortunately, as far as I know,
> validator doesn't provide any facility to accomplish that directly.
> However, I can think of one or two ways you could achieve it.
> 
> The first would be using XML entities to setup and reference your re-used
> validations, something like:
> 
> <DOCTYPE ... [
> <!ENTITY field1   "<field name="loginid" depends="required"...>
> ]>
> ...
> <form ...>
>   &field1;
>   ...
> </form>
> 
> (it may take some fiddling with syntax in the entity definition, and using
> external entities might be easier).
> 
> The second would be to define custom validation rules that encapsulate the
> required composition and parameterization under a single name so that
> everywhere the field is used you'd specify 'depends="my-composite-validation"'.
> 
> If there are any better ways to do this, I'd be interested to hear about
> them too :-)
> 
> L.
> --
> Laurie Harper
> Open Source advocate, Java geek: http://www.holoweb.net/laurie
> Founder, Zotech Software: http://www.zotechsoftware.com/
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

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


RE: Re-use field validations?

Posted by "David G. Friedman" <hu...@ix.netcom.com>.
Leo,

You could also check out Niall's server-side validator
add-on to "extend" a field.  Be aware that doesn't work
for client-side Javascript or any version before 1.2.X.
http://www.niallp.pwp.blueyonder.co.uk/strutsvalidatorextends.html
Again, his extension does allow for adding different 
'groups' (one or more from what I've read) of validations
to any <?form....?><?/form?> set.

Regards,
David

-----Original Message-----
From: Leo Asanov [mailto:leonidasanov@yahoo.com]
Sent: Sunday, September 04, 2005 10:56 PM
To: Struts Users Mailing List
Subject: Re: Re-use field validations?


Hi!

I was looking for this kind of functionality some time
ago and didn't find any standard way of doing reusable
validators. 
What I did was more like a hack, but it worked:

1. For server side validation.

Overwrite validate() method of FormBean class with
something like this:

public ActionErrors validate(ActionMapping
mapping,HttpServletRequest request) {

ServletContext application =
getServlet().getServletContext();

ActionErrors errors = new ActionErrors();

String validationKey = "validationNameIn
Validation.xml";

Validator validator =
Resources.initValidator(validationKey,this,application,
request,errors, page);

try {validatorResults = validator.validate();} 
catch (ValidatorException e) {
logger.error(e.getMessage(), e);
}
return errors;
}	

Just change validationKey to whatever is in your
validation.xml.

2. Client-side validation.

Put the following code at the top of your page:

<script type="text/javascript">
var
realFormName_required=formNameFromValidationXMLFile_required;

</script>

Again replace "realFormName" with the form name how it
appears in your struts-config.xml. And replace
"formNameFromValidationXMLFile" to the validator name
(should be the same one you used for server-side
validation).

Hope this will help.

Cheers,
Leo

> Quinn Stone wrote:
> > Is there any way to define the validation for a
> common form field once in
> > validation.xml and re-use the validation in other
> <form> blocks?


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


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


Re: Re-use field validations?

Posted by Leo Asanov <le...@yahoo.com>.
Hi!

I was looking for this kind of functionality some time
ago and didn't find any standard way of doing reusable
validators. 
What I did was more like a hack, but it worked:

1. For server side validation.

Overwrite validate() method of FormBean class with
something like this:

public ActionErrors validate(ActionMapping
mapping,HttpServletRequest request) {

ServletContext application =
getServlet().getServletContext();

ActionErrors errors = new ActionErrors();

String validationKey = "validationNameIn
Validation.xml";

Validator validator =
Resources.initValidator(validationKey,this,application,
request,errors, page);

try {validatorResults = validator.validate();} 
catch (ValidatorException e) {
logger.error(e.getMessage(), e);
}
return errors;
}	

Just change validationKey to whatever is in your
validation.xml.

2. Client-side validation.

Put the following code at the top of your page:

<script type="text/javascript">
var
realFormName_required=formNameFromValidationXMLFile_required;

</script>

Again replace "realFormName" with the form name how it
appears in your struts-config.xml. And replace
"formNameFromValidationXMLFile" to the validator name
(should be the same one you used for server-side
validation).

Hope this will help.

Cheers,
Leo

> Quinn Stone wrote:
> > Is there any way to define the validation for a
> common form field once in
> > validation.xml and re-use the validation in other
> <form> blocks?


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


Re: Re-use field validations?

Posted by Laurie Harper <la...@holoweb.net>.
Quinn Stone wrote:
> Is there any way to define the validation for a common form field once in
> validation.xml and re-use the validation in other <form> blocks?
> 
> This would allow validation.xml to be updated in one place rather than many
> places, if validation requirements change for a field that is used on many
> forms. It would also help ensure that I (or others) won't accidentally
> incorrectly code different validations for the same field on different forms.
> The benefits will compound if I start writing custom validation routines.
> 
> I am new to Struts, and this is my first project using it, so I hope I'm not
> missing something obvious.

I guess what you're looking for is the ability to specify a list/set of 
validations (e.g. 'depends="required,mask"') and associated parameters, 
messages, etc. once for a field and have that same configuration applied on 
different forms using that field, right? Unfortunately, as far as I know, 
validator doesn't provide any facility to accomplish that directly. 
However, I can think of one or two ways you could achieve it.

The first would be using XML entities to setup and reference your re-used 
validations, something like:

<DOCTYPE ... [
<!ENTITY field1   "<field name="loginid" depends="required"...>
]>
...
<form ...>
   &field1;
   ...
</form>

(it may take some fiddling with syntax in the entity definition, and using 
external entities might be easier).

The second would be to define custom validation rules that encapsulate the 
required composition and parameterization under a single name so that 
everywhere the field is used you'd specify 'depends="my-composite-validation"'.

If there are any better ways to do this, I'd be interested to hear about 
them too :-)

L.
-- 
Laurie Harper
Open Source advocate, Java geek: http://www.holoweb.net/laurie
Founder, Zotech Software: http://www.zotechsoftware.com/


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