You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Schlärmann, Bob <bo...@logica.com> on 2012/06/05 16:51:42 UTC

Conditional validation without overriding isRequired

Hello,

A question about form processing: is it possible to do conditional form validation without overriding FormComponent#isRequired?

I have a form with about 5 different form components (e.g. textfields, dropdown, radiobutton). These fields are only required if a RadioGroup has a certain value.

I've read the solution given in https://cwiki.apache.org/WICKET/conditional-validation.html. But this means I have to subclass all form components  and override isRequired. Is there an easier solution?

Currently I'm using an Ajax call and disable the container of the optional components, however that doesn't work if Javascript is disabled (also not safe presumably).

Best regards,

Bob



Think green - keep it on the screen.

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.


Re: Conditional validation without overriding isRequired

Posted by Andrea Del Bene <ad...@ciseonweb.it>.
Hi,

you can consider to override form's process() method. Before calling the 
super version you can check for the radio value and set the other fields 
as required if the specific value was chosen.
> Thanks for your reply,
>
>> Argh, confused setVisible() with setRequired, my bad ;-)
>> But nevertheless: call setRequiered(condition) in onConfigure().
> I've tried this but if I am correct isRequired() gets called before onConfigure() during the request cycle (at least this is the behaviour I see during debugging).
>
> The problem is that the user is then able to submit the form without filling in the required field if validation completely succeeds at once. Also the required fields "lag" behind one request cycle.
>
>
>>     -Tom
>>
>>
>> Thomas Götz wrote:
>>
>>> Yes, you can also push the state instead of pulling it, which besides is
>> preferrable in terms of efficiency (as onConfigure() is only called once per
>> request whereas isVisible() is potentially called many time):
>>> TextField<String>  textField = new TextField<String>("textField") {
>>>     @Override
>>>     protected void onConfigure() {
>>>         super.onConfigure();
>>>         setVisible(condition);
>>>     }
>>> };
>>>
>>> You can also call textField.setVisible(...) in the Page's or Panel's
>> onConfigure(), but then you need a reference to the textField (property of the
>> page or panel). Or - if possible - you can group your FormComponents into a
>> WebMarkupContainer an set the visibility there.
>>>    -Tom
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>
> Think green - keep it on the screen.
>
> This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


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


RE: Conditional validation without overriding isRequired

Posted by Schlärmann, Bob <bo...@logica.com>.
Thanks for your reply,

> Argh, confused setVisible() with setRequired, my bad ;-)
> But nevertheless: call setRequiered(condition) in onConfigure().

I've tried this but if I am correct isRequired() gets called before onConfigure() during the request cycle (at least this is the behaviour I see during debugging). 

The problem is that the user is then able to submit the form without filling in the required field if validation completely succeeds at once. Also the required fields "lag" behind one request cycle.


> 
>    -Tom
> 
> 
> Thomas Götz wrote:
> 
> > Yes, you can also push the state instead of pulling it, which besides is
> preferrable in terms of efficiency (as onConfigure() is only called once per
> request whereas isVisible() is potentially called many time):
> >
> > TextField<String> textField = new TextField<String>("textField") {
> >    @Override
> >    protected void onConfigure() {
> >        super.onConfigure();
> >        setVisible(condition);
> >    }
> > };
> >
> > You can also call textField.setVisible(...) in the Page's or Panel's
> onConfigure(), but then you need a reference to the textField (property of the
> page or panel). Or - if possible - you can group your FormComponents into a
> WebMarkupContainer an set the visibility there.
> >
> >   -Tom
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 


Think green - keep it on the screen.

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.



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


Re: Conditional validation without overriding isRequired

Posted by Thomas Götz <to...@decoded.de>.
Argh, confused setVisible() with setRequired, my bad ;-)
But nevertheless: call setRequiered(condition) in onConfigure().

   -Tom 


Thomas Götz wrote:

> Yes, you can also push the state instead of pulling it, which besides is preferrable in terms of efficiency (as onConfigure() is only called once per request whereas isVisible() is potentially called many time):
> 
> TextField<String> textField = new TextField<String>("textField") {
>    @Override
>    protected void onConfigure() {
>        super.onConfigure();
>        setVisible(condition);
>    }
> };
> 
> You can also call textField.setVisible(...) in the Page's or Panel's onConfigure(), but then you need a reference to the textField (property of the page or panel). Or - if possible - you can group your FormComponents into a WebMarkupContainer an set the visibility there.
> 
>   -Tom


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


Re: Conditional validation without overriding isRequired

Posted by Thomas Götz <to...@decoded.de>.
Yes, you can also push the state instead of pulling it, which besides is preferrable in terms of efficiency (as onConfigure() is only called once per request whereas isVisible() is potentially called many time):

TextField<String> textField = new TextField<String>("textField") {
    @Override
    protected void onConfigure() {
        super.onConfigure();
        setVisible(condition);
    }
};

You can also call textField.setVisible(...) in the Page's or Panel's onConfigure(), but then you need a reference to the textField (property of the page or panel). Or - if possible - you can group your FormComponents into a WebMarkupContainer an set the visibility there.

   -Tom



On 05.06.2012 at 16:51 Bob Schlärmann wrote:

> Hello,
> 
> A question about form processing: is it possible to do conditional form validation without overriding FormComponent#isRequired?
> 
> I have a form with about 5 different form components (e.g. textfields, dropdown, radiobutton). These fields are only required if a RadioGroup has a certain value.
> 
> I've read the solution given in https://cwiki.apache.org/WICKET/conditional-validation.html. But this means I have to subclass all form components  and override isRequired. Is there an easier solution?
> 
> Currently I'm using an Ajax call and disable the container of the optional components, however that doesn't work if Javascript is disabled (also not safe presumably).
> 
> Best regards,
> 
> Bob


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