You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Michael Sparer <mi...@gmx.at> on 2007/10/09 02:49:58 UTC

Decouple parts of a form

Hi folks, 

I've been fiddling around with JCaptcha, Wicket and Spring these days. Now
I'm trying to decouple the captcha part of the GUI from the rest. I.e. I'm
trying to construct a captcha-component that can easily be nested into a
form (into any form basically).
The captcha class should look something like that

public abstract class CaptchaPanel extends Panel {
    ....

    public CaptchaPanel() {
        Form form;
        add(form = new Form("captchaForm") {
              protected void onSubmit() {
                  // do the captcha validation stuff

                  // call on error if the entered tet
                  Captcha.this.onError();
              }
        });
        // add the components
        form.add(new RequiredTextField("captchaInput" .... );
        form.add(new Image(........));
    }
    
    protected abstract void onError();
}

I know from the wicket 1.3 API that forms can be nested and I also know that
each form has to be submitted separately. 
My problem therefore is that the captcha-form's onSubmit button doesn't get
called if someone submits the outside form (= the form the component is
nested in). Is there a possibility to submit the 'inner' form as well when
the 'outer' form is submitted? I'm also open for other suggestions, maybe
there's already a Wicket best-practise solution, which would be nice :-)

I'm kinda stuck, it's late at night and I really hope someone is able to
give me a hint.

Thanks in advance

Michael
-- 
View this message in context: http://www.nabble.com/Decouple-parts-of-a-form-tf4591493.html#a13107429
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Decouple parts of a form

Posted by Martijn Dashorst <ma...@gmail.com>.
Wicket allows nested forms because one component doesn't know if it is
inside a form or not. This is typical for component frameworks that
try to maximize component reuse and encapsulation. That the HTML spec
says otherwise doesn't mean a lot to us, as we are looking at the
Component part.

The child forms are replaced with span tags, and the whole form is
submitted. The only values that are actually processed are the fields
that are part of the child form. The rest keeps its input but that is
the only thing that happens.

Martijn

On 10/9/07, martinf <fu...@arcor.de> wrote:
>
>
>
> Michael Sparer wrote:
> >
> > I know from the wicket 1.3 API that forms can be nested and I also know
> > that each form has to be submitted separately.
> > My problem therefore is that the captcha-form's onSubmit button doesn't
> > get called if someone submits the outside form (= the form the component
> > is nested in). Is there a possibility to submit the 'inner' form as well
> > when the 'outer' form is submitted? I'm also open for other suggestions,
> > maybe there's already a Wicket best-practise solution, which would be nice
> > :-)
> >
> Reading
> http://www.w3.org/TR/html401/interact/forms.html#h-17.3
> plus the interpretation, two lines above, in this link:
> http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.3.4
> I'd say, maybe wicket alows it, but in html they can't.
>
> But maybe the capacha don't need its own form as all form components are
> submitted, so just the components representing the capacha need to be added
> to the form. To encapsulate those FormComponentPanel might be of help.
>
> Martin
>
> --
> View this message in context:
> http://www.nabble.com/Decouple-parts-of-a-form-tf4591493.html#a13111035
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Buy Wicket in Action: http://manning.com/dashorst
Apache Wicket 1.3.0-beta3 is released
Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.0-beta3/

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Decouple parts of a form

Posted by Michael Sparer <mi...@gmx.at>.
Thanks a lot for your input guys, it really helped me a lot. I chose to use
Kent's approach using a Validator that - by the way - could not be a static
class as I had to access member variables outside the Validator.

Thanks again and have a nice day

Michael


Kent Tong wrote:
> 
> 
> Gerolf Seitz wrote:
>> 
>> ditch the form and let CaptchaPanel extend FormComponentPanel. for the
>> captcha and do a check for the form in onBeforeRender (since it's not yet
>> added to the hierarchy in the constructor).
>> just use "getForm();" to check for the existence of an ancestor form.
>> this
>> method throws an exception if no form was found.
>> 
> 
> I'd suggest that it extend Panel, not FormComponentPanel as there is no
> composite data structure here. The best place to validate the input is
> in a custom validator. Here is some sample code (there is also step-by-
> step instruction on validation in my tutorials; see my signature):
> 
> public class CaptchaPanel extends Panel {
> 	private static class CaptchaValidator extends AbstractValidator {
> 		protected void onValidate(IValidatable validatable) {
> 			String value = (String) validatable.getValue();
> 			if (value matches the image) {
> 				return;
> 			}
> 			error(validatable);
> 		}
> 	}
> 	public CaptchaPanel(String id) {
> 		super(id);
> 		RequiredTextField t = new RequiredTextField("captchaInput", new
> Model());
> 		t.add(new CaptchaValidator());
> 		add(t);
> 		add(new Image("captchaImage", ...));
> 	}
> }
> 
> 

-- 
View this message in context: http://www.nabble.com/Decouple-parts-of-a-form-tf4591493.html#a13114625
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Decouple parts of a form

Posted by Kent Tong <ke...@cpttm.org.mo>.

Gerolf Seitz wrote:
> 
> ditch the form and let CaptchaPanel extend FormComponentPanel. for the
> captcha and do a check for the form in onBeforeRender (since it's not yet
> added to the hierarchy in the constructor).
> just use "getForm();" to check for the existence of an ancestor form. this
> method throws an exception if no form was found.
> 

I'd suggest that it extend Panel, not FormComponentPanel as there is no
composite data structure here. The best place to validate the input is
in a custom validator. Here is some sample code (there is also step-by-
step instruction on validation in my tutorials; see my signature):

public class CaptchaPanel extends Panel {
	private static class CaptchaValidator extends AbstractValidator {
		protected void onValidate(IValidatable validatable) {
			String value = (String) validatable.getValue();
			if (value matches the image) {
				return;
			}
			error(validatable);
		}
	}
	public CaptchaPanel(String id) {
		super(id);
		RequiredTextField t = new RequiredTextField("captchaInput", new Model());
		t.add(new CaptchaValidator());
		add(t);
		add(new Image("captchaImage", ...));
	}
}


-----
--
Kent Tong
Wicket tutorials freely available at http://www.agileskills2.org/EWDW
-- 
View this message in context: http://www.nabble.com/Decouple-parts-of-a-form-tf4591493.html#a13112961
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Decouple parts of a form

Posted by Gerolf Seitz <ge...@gmail.com>.
i agree with martin,

ditch the form and let CaptchaPanel extend FormComponentPanel. for the
captcha and do a check for the form in onBeforeRender (since it's not yet
added to the hierarchy in the constructor).
just use "getForm();" to check for the existence of an ancestor form. this
method throws an exception if no form was found.

gerolf

On 10/9/07, martinf <fu...@arcor.de> wrote:
>
>
>
>
> Michael Sparer wrote:
> >
> > I know from the wicket 1.3 API that forms can be nested and I also know
> > that each form has to be submitted separately.
> > My problem therefore is that the captcha-form's onSubmit button doesn't
> > get called if someone submits the outside form (= the form the component
> > is nested in). Is there a possibility to submit the 'inner' form as well
> > when the 'outer' form is submitted? I'm also open for other suggestions,
> > maybe there's already a Wicket best-practise solution, which would be
> nice
> > :-)
> >
> Reading
> http://www.w3.org/TR/html401/interact/forms.html#h-17.3
> plus the interpretation, two lines above, in this link:
> http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.3.4
> I'd say, maybe wicket alows it, but in html they can't.
>
> But maybe the capacha don't need its own form as all form components are
> submitted, so just the components representing the capacha need to be
> added
> to the form. To encapsulate those FormComponentPanel might be of help.
>
> Martin
>
> --
> View this message in context:
> http://www.nabble.com/Decouple-parts-of-a-form-tf4591493.html#a13111035
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Decouple parts of a form

Posted by martinf <fu...@arcor.de>.


Michael Sparer wrote:
> 
> I know from the wicket 1.3 API that forms can be nested and I also know
> that each form has to be submitted separately. 
> My problem therefore is that the captcha-form's onSubmit button doesn't
> get called if someone submits the outside form (= the form the component
> is nested in). Is there a possibility to submit the 'inner' form as well
> when the 'outer' form is submitted? I'm also open for other suggestions,
> maybe there's already a Wicket best-practise solution, which would be nice
> :-)
> 
Reading
http://www.w3.org/TR/html401/interact/forms.html#h-17.3
plus the interpretation, two lines above, in this link:
http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.3.4
I'd say, maybe wicket alows it, but in html they can't.

But maybe the capacha don't need its own form as all form components are
submitted, so just the components representing the capacha need to be added
to the form. To encapsulate those FormComponentPanel might be of help.

Martin

-- 
View this message in context: http://www.nabble.com/Decouple-parts-of-a-form-tf4591493.html#a13111035
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org