dynamic form validation

2 views
Skip to first unread message

Ksenia Marasanova

unread,
Sep 7, 2006, 8:06:10 PM9/7/06
to turbo...@googlegroups.com
Hi,

I want to use one controller method( "save") for both creating and
updating a record. I have two methods for displaying a form: "add" and
"edit". The problem is that the form for adding and editing are
slightly different (different validator in this case) . I can
instantiate two forms, but I can pass only one instance to @validate
decorator of "save" method.
Are separate methods for creating and updating record the only way out?


Appreciate any hints!

Thanks
Ksenia

Timur Izhbulatov

unread,
Sep 8, 2006, 2:34:24 AM9/8/06
to turbo...@googlegroups.com

I'm facing a similar problem right now [1]. The solution I found is creating my
own decorator for validation. It receives a callable which generates the schema
for validation. I could send you some snippets if you need.

But I believe in your case it would be better to use separate methods. What is
the point of using single method? Code reuse? You could achieve it by extracting
common parts into separate reusable methods.

[1] http://groups.google.com/group/turbogears/browse_frm/thread/0f57bc6b3dc3caa5/

--
Timur Izhbulatov
OILspace, 26 Leninskaya sloboda, bld. 2, 2nd floor, 115280 Moscow, Russia
P:+7 495 105 7245 + ext.205 F:+7 495 105 7246 E:TimurIz...@oilspace.com
Building Successful Supply Chains - One Solution At A Time.
www.oilspace.com

Max Ischenko

unread,
Sep 8, 2006, 6:36:27 AM9/8/06
to TurboGears

Two ideas:

1. You can pass a callable to
@turbogears.validate(form=...)
that would return appropriate form. Of course, to select appropriate
form you need info from a form submitted which is not available from a
callable directly. Though you may check HTTP referer header.

2. You can validate inside your controller's method. Not as pretty but
it works.

Max.

Ksenia Marasanova

unread,
Sep 8, 2006, 10:41:53 AM9/8/06
to turbo...@googlegroups.com
Timur and Max,

thanks for the tips,

On 9/8/06, Timur Izhbulatov <ti...@oilspace.com> wrote:
> I'm facing a similar problem right now [1]. The solution I found is creating my
> own decorator for validation. It receives a callable which generates the schema
> for validation. I could send you some snippets if you need.

Thanks, I'd like to see that! Or maybe you can put your code on the wiki?

>
> But I believe in your case it would be better to use separate methods. What is
> the point of using single method? Code reuse? You could achieve it by extracting
> common parts into separate reusable methods.
>

You are right i can do that. For this particular case it's more a
habbit of me for the /add/edit/save url pattern :)


On 9/8/06, Max Ischenko <isch...@gmail.com> wrote:
>
>
> Two ideas:
>
> 1. You can pass a callable to
> @turbogears.validate(form=...)
> that would return appropriate form. Of course, to select appropriate
> form you need info from a form submitted which is not available from a
> callable directly. Though you may check HTTP referer header.

I've noticed that @validate accepts a callable, but didnt think about
using referer! Good one, thanks.

>
> 2. You can validate inside your controller's method. Not as pretty but
> it works.
>

True, but this is the thing I like the most in TG - widgets with
validators separated from controller code :)


thanks,
Ksenia.

Timur Izhbulatov

unread,
Sep 9, 2006, 6:39:31 AM9/9/06
to turbo...@googlegroups.com
On Fri, Sep 08, 2006 at 04:41:53PM +0200, Ksenia Marasanova wrote:
> On 9/8/06, Timur Izhbulatov <ti...@oilspace.com> wrote:
> > I'm facing a similar problem right now [1]. The solution I found is creating my
> > own decorator for validation. It receives a callable which generates the schema
> > for validation. I could send you some snippets if you need.
>
> Thanks, I'd like to see that! Or maybe you can put your code on the wiki?

You mean http://trac.turbogears.org/turbogears/wiki/?
I could do this on Monday. But you only need your own @validate for something
really special.

What about me, I've found the way to get rid of my own decorator
(http://groups.google.com/group/turbogears/tree/browse_frm/thread/0f57bc6b3dc3caa5/358a92ca7d83ecfe?rnum=1&_done=%2Fgroup%2Fturbogears%2Fbrowse_frm%2Fthread%2F0f57bc6b3dc3caa5%2F%3F#doc_0829c3fb0ecfc6fe)

Timur Izhbulatov

unread,
Sep 13, 2006, 4:49:45 PM9/13/06
to turbo...@googlegroups.com
On Sat, Sep 09, 2006 at 02:39:31PM +0400, Timur Izhbulatov wrote:
>
> On Fri, Sep 08, 2006 at 04:41:53PM +0200, Ksenia Marasanova wrote:
> > On 9/8/06, Timur Izhbulatov <ti...@oilspace.com> wrote:
> > > I'm facing a similar problem right now [1]. The solution I found is creating my
> > > own decorator for validation. It receives a callable which generates the schema
> > > for validation. I could send you some snippets if you need.
> >
> > Thanks, I'd like to see that! Or maybe you can put your code on the wiki?
>
> You mean http://trac.turbogears.org/turbogears/wiki/?
> I could do this on Monday. But you only need your own @validate for something
> really special.
>
> What about me, I've found the way to get rid of my own decorator
> (http://groups.google.com/group/turbogears/tree/browse_frm/thread/0f57bc6b3dc3caa5/358a92ca7d83ecfe?rnum=1&_done=%2Fgroup%2Fturbogears%2Fbrowse_frm%2Fthread%2F0f57bc6b3dc3caa5%2F%3F#doc_0829c3fb0ecfc6fe)

OK. Here is the summary:
http://trac.turbogears.org/turbogears/wiki/DynamicValidation

Max Ischenko

unread,
Sep 14, 2006, 2:06:41 AM9/14/06
to TurboGears

Timur Izhbulatov wrote:

Cool. Though overly complicated to my own taste. Hopefully TG 1.1/2.0
will make implementing dynamic validation a bit easier.

--
Max Ischenko
http://www.developers.org.ua -- Ukranian software developers community

Max Ischenko

unread,
Sep 14, 2006, 2:06:47 AM9/14/06
to TurboGears

Timur Izhbulatov wrote:

Cool. Though overly complicated to my own taste. Hopefully TG 1.1/2.0


will make implementing dynamic validation a bit easier.

--
Max Ischenko
http://www.developers.org.ua -- Ukrainian software developers community

Timur Izhbulatov

unread,
Sep 14, 2006, 4:25:05 AM9/14/06
to turbo...@googlegroups.com
On Wed, Sep 13, 2006 at 11:06:41PM -0700, Max Ischenko wrote:
>
> Timur Izhbulatov wrote:
>
> > OK. Here is the summary:
> > http://trac.turbogears.org/turbogears/wiki/DynamicValidation
>
> Cool. Though overly complicated to my own taste.

Yeah, kind of. I couldn't find a simpler way. And the task itself is rather
complex too so I took the tradeoff between complexity and flexibility.

> Hopefully TG 1.1/2.0
> will make implementing dynamic validation a bit easier.

I can't make any assumptions on this, my imagination refuses to work =)

Widgets look good as general approach for me but still far from perfection when
it comes to form rendering, and they are tightly coupled with Kid.

Reply all
Reply to author
Forward
0 new messages