newforms: emptying fields after failed validation

32 views
Skip to first unread message

Gollum

unread,
Feb 25, 2008, 12:25:13 PM2/25/08
to Django users
I use form.clean() method to validate two fields of my form and I want
those fields to be empty when the form is shown again.
The problem is I don't know how to set fields from clean() method.
Setting cleaned_data['field1'] = '' and returning cleaned_data from
clean() method doesn't seem to work.

Karen Tracey

unread,
Feb 25, 2008, 1:30:44 PM2/25/08
to django...@googlegroups.com
On Mon, Feb 25, 2008 at 12:25 PM, Gollum <zgo...@gmail.com> wrote:
I use form.clean() method to validate two fields of my form and I want
those fields to be empty when the form is shown again.

Aside: as a user, I find forms that do this annoying, since I can no longer see what it is the form error is complaining about.  Unless you are talking about blanking mis-matched obfuscated password fields, in which case it saves me the step of erasing what's already there that is already completely useless since I can't see it anyway.
 
The problem is I don't know how to set fields from clean() method
Setting cleaned_data['field1'] = '' and returning cleaned_data from
clean() method doesn't seem to work.

From: http://www.djangoproject.com/documentation/newforms/#creating-form-instances

"If you have a bound Form instance and want to change the data somehow, or if you want to bind an unbound Form instance to some data, create another Form instance. There is no way to change data in a Form instance. Once a Form instance has been created, you should consider its data immutable, whether it has data or not."

Which sounds like you can't easily do what you want.  Presumably you'd want to keep the error messages raised by validation, and any data that did validate.  I guess you could cycle through the invalid form's _errors dictionary and remove those keys from (a copy of?) the request.POST dictionary, instantiate a new form using the stripped-down POST dictionary, copy over _errors from the invalid form to the new one, and then display the result.  Seems pretty hackish, though, which is likely an indication that either I'm missing the elegant way to do it, or it is not something Django's forms were designed to do.

Karen

Gollum

unread,
Feb 26, 2008, 8:50:32 AM2/26/08
to Django users
Thanks for a very useful answer, Karen!
Clearing obfuscated password fields is exactly what I'm trying to
do :)

Karen Tracey:
> On Mon, Feb 25, 2008 at 12:25 PM, Gollum <zgo...@gmail.com> wrote:
>
> > I use form.clean() method to validate two fields of my form and I want
> > those fields to be empty when the form is shown again.
>
>
> Aside: as a user, I find forms that do this annoying, since I can no longer
> see what it is the form error is complaining about. Unless you are talking
> about blanking mis-matched obfuscated password fields, in which case it
> saves me the step of erasing what's already there that is already completely
> useless since I can't see it anyway.
>
>
> > The problem is I don't know how to set fields from clean() method
> > Setting cleaned_data['field1'] = '' and returning cleaned_data from
> > clean() method doesn't seem to work.
> >
>
> From:
> http://www.djangoproject.com/documentation/newforms/#creating-form-instances
>
> "If you have a bound Form instance and want to change the data somehow, or
> if you want to bind an unbound Form instance to some data, create another
> Form instance. There is no way to change data in a Form instance. Once a
> Form instance has been created, you should consider its data immutable,
> whether it has data or not."
>
> Which sounds like you can't easily do what you want. Presumably you'd want
> to keep the error messages raised by validation, and any data that did
> validate. I guess you could cycle through the invalid form's _errors
> dictionary and remove those keys from (a copy of?) the
> request.POSTdictionary, instantiate a new form using the stripped-down

SmileyChris

unread,
Feb 26, 2008, 5:21:07 PM2/26/08
to Django users


On Feb 27, 2:50 am, Gollum <zgol...@gmail.com> wrote:
> Thanks for a very useful answer, Karen!
> Clearing obfuscated password fields is exactly what I'm trying to
> do :)

For the specific case of clearing password fields, there's an option
for that already:

password = CharField(widget=PasswordInput(render_value=False))

Karen Tracey

unread,
Feb 26, 2008, 5:46:39 PM2/26/08
to django...@googlegroups.com

Now that's useful.  Am I missing where it's documented?  If not, should it be documented?

Karen

SmileyChris

unread,
Feb 26, 2008, 9:44:33 PM2/26/08
to Django users
On Feb 27, 11:46 am, "Karen Tracey" <kmtra...@gmail.com> wrote:
> Now that's useful. Am I missing where it's documented? If not, should it
> be documented?

Yes, it should be. You wanna open the ticket?

Karen Tracey

unread,
Feb 27, 2008, 8:18:51 AM2/27/08
to django...@googlegroups.com

Oh, sure.  Here it is: http://code.djangoproject.com/ticket/6674

Cheers,
Karen 

Gollum

unread,
Apr 2, 2008, 9:44:22 AM4/2/08
to Django users
Still, does anybody know how to actually change fields value after
failed validation.
I really only need to clear my password fields only when the passwords
do not validate, not on any invalid field.

Karen Tracey

unread,
Apr 2, 2008, 1:40:19 PM4/2/08
to django...@googlegroups.com

I'm confused, does the method supplied by SmilleyChris:


> For the specific case of clearing password fields, there's an option
> for that already:

> password = CharField(widget=PasswordInput(render_value=False))

not work?  It sound like exactly what you want to do?

Karen

Reply all
Reply to author
Forward
0 new messages