deform: Support for readonly on individual form fields?

83 views
Skip to first unread message

keesjan

unread,
Jul 30, 2011, 7:19:08 AM7/30/11
to pylons-discuss
As far as I understand, deform now has an all-or-nothing approach for
readonly fields at the form level.

There're occasions, though, when you want a form where only some
fields are editable, but others should remain readonly. The most
obvious example is a persisted ID field.

Ideally, I like to have the option to pass a readonly flag to the
widget, like so:

form['id'].widget = TextInputWidget(readonly=True)

Unfortunately, this is currently not supported.

Is this a sensible feature request or are there other ways to achieve
the same result?!

(A TextDisplayWidget is an obvious alternative, but this is very crude
and doesn't fit in nicely with the readonly support already given.)


Kind regards,

Keesjan

Dirley

unread,
Jul 30, 2011, 12:28:05 PM7/30/11
to pylons-...@googlegroups.com
I've once tried something like this, but was (partially) unsuccessful.

The thing is that you build your form with some data, and later when the data comes back, you will want to process and validate the received data, right? But how can you do that if you only received partial data -- because as some fields are readonly, their data will not be in the received dataset?

Imagine you have a required-and-readonly field. You draw the form, but when the user submits the form, that field's data will not come back (as its a readonly field) and you'll get a "required" error.

I've hacked around this limitation by monkeypatching some methods and defining "missing" together with "readonly". But I think that it can be hard to put it all together in colander and deform.

If you guys have any idea, I would like to know too. :)

2011/7/30 keesjan <k.vanbu...@inter.nl.net>

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.


Tim Hoffman

unread,
Jul 30, 2011, 10:42:10 PM7/30/11
to pylons-...@googlegroups.com
There are a couple of stratehies but dependant on javascript for at least one.

1.  libs like dojo allow you mark widgets as readonly so you get the same look and feel in most situations (though obviously not the same as with select elements)
2. include the field as readonly.

In both cases though you are assuming you can trust the incoming data hasn't been fiddled with in terms of the readonly data.

I faced a similiar problem using formish and solved it by getting readonly fields to just display, and using session data to populate the missing fields
prior to validation.

Rgds

T

Tim Hoffman

unread,
Jul 30, 2011, 10:42:58 PM7/30/11
to pylons-...@googlegroups.com
OOps the second option should read include the field as hidden.
I should learn to re-read my posts before pressing the "Post" button ;-)

T

Rapier Bsd

unread,
Aug 3, 2011, 7:38:42 AM8/3/11
to pylons-discuss
You can do something like:
Assuming you have two schema nodes in your schema:
foo and bar, you can have a readonly foo like this:

schema = Schema()
form = Form(schema)
form.set_widgets({'foo':widget.TextInputWidget(template='readonly/
textinput')})
Reply all
Reply to author
Forward
0 new messages