You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ben <ne...@gmail.com> on 2004/12/09 22:57:17 UTC

Nested form validation

Hi

Within my normal form there is a nested form. The nested form has
custom validation. The validate attribute of the action is set to
false. My question is how do I validate the normal form and
automatically validate the nested form with my custom validation
method?

E.g.

NormalForm () {
NestedForm nested = new NestedForm();
}

NestedForm () {
validate(...) {
// checking the data here
}
}

So my action would validate the form as:

MyAction () {
NormalForm nForm = (NormalForm) form;
ActionErrors errors = nForm.validate(); // Validate the data
}

The problem here is that it validates the NormalForm and ignores my
validation in the NestedForm.

I hope this question makes sense to you... :)

Thanks,
Ben

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


Re: Nested form validation

Posted by Ben <ne...@gmail.com>.
Thanks, I am doing just that.

Just a quick question, how do I use the property name as the error key
in the validate() method?

I have this but it uses the form name as the key, not the name of the
property currently validating.

public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request) {
     ActionErrors errors = new ActionErrors();
     String validationKey = getValidationKey(mapping, request);
     
     errors.add(validationKey, new ActionMessage("errors.input"));
     
     return errors;
}

Thanks

On Fri, 10 Dec 2004 09:34:26 -0600, Hubert Rabago <hr...@gmail.com> wrote:
> I would think so, but you'd have to call Validator yourself.  Take a
> look at how the ValidatorForm#validate() calls it, and base your
> nested.validate() code on that.  After the call, append the errors
> from your main form and those in your nested form.
> 
> 
> 
> On Fri, 10 Dec 2004 10:41:36 +1100, Ben <ne...@gmail.com> wrote:
> > If I do this, can I use Struts validators and my custom validate method?
> >
> >
> >
> >
> > On Thu, 9 Dec 2004 16:08:03 -0600, Hubert Rabago <hr...@gmail.com> wrote:
> > > Why not call nested.validate() inside NormalForm#validate(), then add
> > > the appropriate ActionMessage objects in the resulting ActionErrors?
> > >
> > >
> > >
> > >
> > > On Fri, 10 Dec 2004 08:57:17 +1100, Ben <ne...@gmail.com> wrote:
> > > > Hi
> > > >
> > > > Within my normal form there is a nested form. The nested form has
> > > > custom validation. The validate attribute of the action is set to
> > > > false. My question is how do I validate the normal form and
> > > > automatically validate the nested form with my custom validation
> > > > method?
> > > >
> > > > E.g.
> > > >
> > > > NormalForm () {
> > > > NestedForm nested = new NestedForm();
> > > > }
> > > >
> > > > NestedForm () {
> > > > validate(...) {
> > > > // checking the data here
> > > > }
> > > > }
> > > >
> > > > So my action would validate the form as:
> > > >
> > > > MyAction () {
> > > > NormalForm nForm = (NormalForm) form;
> > > > ActionErrors errors = nForm.validate(); // Validate the data
> > > > }
> > > >
> > > > The problem here is that it validates the NormalForm and ignores my
> > > > validation in the NestedForm.
> > > >
> > > > I hope this question makes sense to you... :)
> > > >
> > > > Thanks,
> > > > Ben
> > > >
> > > > ---------------------------------------------------------------------
> > >
> > >
> > > > 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
> 
>

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


Re: Nested form validation

Posted by Hubert Rabago <hr...@gmail.com>.
I would think so, but you'd have to call Validator yourself.  Take a
look at how the ValidatorForm#validate() calls it, and base your
nested.validate() code on that.  After the call, append the errors
from your main form and those in your nested form.

On Fri, 10 Dec 2004 10:41:36 +1100, Ben <ne...@gmail.com> wrote:
> If I do this, can I use Struts validators and my custom validate method?
> 
> 
> 
> 
> On Thu, 9 Dec 2004 16:08:03 -0600, Hubert Rabago <hr...@gmail.com> wrote:
> > Why not call nested.validate() inside NormalForm#validate(), then add
> > the appropriate ActionMessage objects in the resulting ActionErrors?
> >
> >
> >
> >
> > On Fri, 10 Dec 2004 08:57:17 +1100, Ben <ne...@gmail.com> wrote:
> > > Hi
> > >
> > > Within my normal form there is a nested form. The nested form has
> > > custom validation. The validate attribute of the action is set to
> > > false. My question is how do I validate the normal form and
> > > automatically validate the nested form with my custom validation
> > > method?
> > >
> > > E.g.
> > >
> > > NormalForm () {
> > > NestedForm nested = new NestedForm();
> > > }
> > >
> > > NestedForm () {
> > > validate(...) {
> > > // checking the data here
> > > }
> > > }
> > >
> > > So my action would validate the form as:
> > >
> > > MyAction () {
> > > NormalForm nForm = (NormalForm) form;
> > > ActionErrors errors = nForm.validate(); // Validate the data
> > > }
> > >
> > > The problem here is that it validates the NormalForm and ignores my
> > > validation in the NestedForm.
> > >
> > > I hope this question makes sense to you... :)
> > >
> > > Thanks,
> > > Ben
> > >
> > > ---------------------------------------------------------------------
> >
> >
> > > 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: Nested form validation

Posted by Hubert Rabago <hr...@gmail.com>.
Why not call nested.validate() inside NormalForm#validate(), then add
the appropriate ActionMessage objects in the resulting ActionErrors?


On Fri, 10 Dec 2004 08:57:17 +1100, Ben <ne...@gmail.com> wrote:
> Hi
> 
> Within my normal form there is a nested form. The nested form has
> custom validation. The validate attribute of the action is set to
> false. My question is how do I validate the normal form and
> automatically validate the nested form with my custom validation
> method?
> 
> E.g.
> 
> NormalForm () {
> NestedForm nested = new NestedForm();
> }
> 
> NestedForm () {
> validate(...) {
> // checking the data here
> }
> }
> 
> So my action would validate the form as:
> 
> MyAction () {
> NormalForm nForm = (NormalForm) form;
> ActionErrors errors = nForm.validate(); // Validate the data
> }
> 
> The problem here is that it validates the NormalForm and ignores my
> validation in the NestedForm.
> 
> I hope this question makes sense to you... :)
> 
> Thanks,
> Ben
> 
> ---------------------------------------------------------------------
> 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: Nested form validation

Posted by Matt Bathje <mp...@ntsource.com>.
Ben wrote:
> I meant this:
> 
> <form>
>   <input type="text" name="name" value="$name">
>   <input type="text" name="city" value="$address.city">
>   <input type="text" name="zipcode" value="$address.zipcode">
>   <input type="text" name="country" value="$address.country">
> </form>
> 
> UserForm () {
> String name;
> AddressForm address = new AddressForm();
> }
> 
> AddressForm () {
> String city;
> String zipcode;
> String country;
> 
> validate(...) {
> // validate the address
> }
> }
> 
> UserAction () {
> UserForm userForm = (UserForm) form;
> ActionErrors errors = userForm.validate(); // Manually validate the data
> }
> 
> When I call the validate method in my action to validate the userForm,
> I would like it to validate the addressForm as well (with my custom
> validate method).
> 
> Can I do something like this?
> 
> ActionErrors errors = userForm.validate(); // Validate the userForm
> errors = userForm.address.validate(); // Validate the addressForm
> 
> So the errors variable would contains errors in userForm and addressForm.
> 
> Thanks,
> Ben
> 

Ben - I think you have it mostly correct, except that the call to 
userForm.address.validate(); should APPEND to errors, not overwrite it 
by using an equal sign.

Hopefully somebody will correct anything else that won't work :)


Matt

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


Re: Nested form validation

Posted by Ben <ne...@gmail.com>.
I meant this:

<form>
  <input type="text" name="name" value="$name">
  <input type="text" name="city" value="$address.city">
  <input type="text" name="zipcode" value="$address.zipcode">
  <input type="text" name="country" value="$address.country">
</form>

UserForm () {
String name;
AddressForm address = new AddressForm();
}

AddressForm () {
String city;
String zipcode;
String country;

validate(...) {
// validate the address
}
}

UserAction () {
UserForm userForm = (UserForm) form;
ActionErrors errors = userForm.validate(); // Manually validate the data
}

When I call the validate method in my action to validate the userForm,
I would like it to validate the addressForm as well (with my custom
validate method).

Can I do something like this?

ActionErrors errors = userForm.validate(); // Validate the userForm
errors = userForm.address.validate(); // Validate the addressForm

So the errors variable would contains errors in userForm and addressForm.

Thanks,
Ben


On Thu, 09 Dec 2004 16:01:12 -0600, Matt Bathje <mp...@ntsource.com> wrote:
> Are you talking something like
> 
> <form>
>    <input type="text" name="bla">
>    <form>
>      <input type="text" name="form2bla">
>    </form>
> </form>
> 
> If so, then its most likely not possible. Furthermore, it is not
> standard HTML, and not guaranteed to work on any browser - whatever you
> are doing with nested forms needs to be rethought so as not to use them.
> 
> If that is not what you are talking about, we may need further
> explanation (with possibly a sample of the form...)
> 
> Matt
> 
> 
> 
> 
> Ben wrote:
> > Hi
> >
> > Within my normal form there is a nested form. The nested form has
> > custom validation. The validate attribute of the action is set to
> > false. My question is how do I validate the normal form and
> > automatically validate the nested form with my custom validation
> > method?
> >
> > E.g.
> >
> > NormalForm () {
> > NestedForm nested = new NestedForm();
> > }
> >
> > NestedForm () {
> > validate(...) {
> > // checking the data here
> > }
> > }
> >
> > So my action would validate the form as:
> >
> > MyAction () {
> > NormalForm nForm = (NormalForm) form;
> > ActionErrors errors = nForm.validate(); // Validate the data
> > }
> >
> > The problem here is that it validates the NormalForm and ignores my
> > validation in the NestedForm.
> >
> > I hope this question makes sense to you... :)
> >
> > Thanks,
> > Ben
> > 
> > ---------------------------------------------------------------------
> > 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
> 
>

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


Re: Nested form validation

Posted by Matt Bathje <mp...@ntsource.com>.
Are you talking something like

<form>
   <input type="text" name="bla">
   <form>
     <input type="text" name="form2bla">
   </form>
</form>


If so, then its most likely not possible. Furthermore, it is not 
standard HTML, and not guaranteed to work on any browser - whatever you 
are doing with nested forms needs to be rethought so as not to use them.

If that is not what you are talking about, we may need further 
explanation (with possibly a sample of the form...)


Matt



Ben wrote:
> Hi
> 
> Within my normal form there is a nested form. The nested form has
> custom validation. The validate attribute of the action is set to
> false. My question is how do I validate the normal form and
> automatically validate the nested form with my custom validation
> method?
> 
> E.g.
> 
> NormalForm () {
> NestedForm nested = new NestedForm();
> }
> 
> NestedForm () {
> validate(...) {
> // checking the data here
> }
> }
> 
> So my action would validate the form as:
> 
> MyAction () {
> NormalForm nForm = (NormalForm) form;
> ActionErrors errors = nForm.validate(); // Validate the data
> }
> 
> The problem here is that it validates the NormalForm and ignores my
> validation in the NestedForm.
> 
> I hope this question makes sense to you... :)
> 
> Thanks,
> Ben
> 
> ---------------------------------------------------------------------
> 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