Tuesday, May 26, 2015

Re: Changing a form field's value during cleaning

Because it is possible that user A will load the page, and user B will approve the field.

On 27 May 2015 03:38, "Vijay Khemlani" <vkhemlan@gmail.com> wrote:
If some of the fields are "already approved" and thus they can't be edited, why are you rendering those as editable form fields in the first place?

On Tue, May 26, 2015 at 9:55 PM, Gergely Polonkai <gergely@polonkai.eu> wrote:

Hello,

I'm currently having hard times changing a form field's value from within the form's clean() method. I have already tried setting self.initial[field_id] and manipulating the cleaned data by calling super's clean method, change the required field (cleaned_data[field_id]) and returning the modified dict, but all for no effect. Trying to change self.data threw me an error saying data is immutable. Question is, is it even possible, even if I had to tamper with "protected/private" properties/methods?

Should I be on the wrong track, I try to outline the problem:

I have a form with fields added programmatically in __init__(). The value of these fields come from one of my models. It is highly possible that another user puts one of the fields in approved state. If this happens, I don't want the current user to be able to change its value, hence I raise a validation error in the field's validate() method.

Now comes the tricky part. In this particular case I want three things to happen:

• warn the user about the fact that the field has already been approved (i.e. display the validation error message)
• render the input field as read only so the user can't modify it (well, at least not in a trivial way)
• and revert the field's value to the one in the database

However, in case of a validation error the field's value remains the "illegal" one the user just entered, and, as the input field is rendered read only, there's no way for him to change it. Thus, I am trying to set it during MyForm.clean() by walking through field errors looking form the proper error code ('already_approved'), and once found, set it back to its database value. This way I hope that my ValidationError remains (hence the warning on the page) while resetting the unchangeable value so at the time of the next submit it won't cause a problem.

Kind regards,
Gergely

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACczBUK2EEaHBaS7bS%3DJF8Ce35MBkrwugtRvZCJC9E_Ovk2Rqg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei2dZX1KWd-SQ1S9VJ%3DbK8bN9Xj1MKvb7jS-zNyvxSz3Aw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACczBUJ32cOC09_oJ8WyZ_VciA9-tQgQ1T2MF4QMCf4QZ5qqnQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment