Setting a form field's value during validation
I read about this issue already, but I'm having trouble understanding why I can't change the value of a form's field during validation.
I have a form where a user can enter a decimal value. This value has to be higher than the initial value of the item the user is changing. During clean(), the value that was entered is checked against the item's previous value. I would like to be able to 开发者_运维百科re-set the form field's value to the item's initial value when a user enters a lower value.
Is this possible from within the clean() method, or am I forced to do this in the view? Somehow, it doesn't feel right to do this in the view...
(To make matters more complicated, the form's fields are built up dynamically, meaning I have to override the form's clean() method instead of using the clean_() method).
I agree with Jack M's comment above. However, if you are going to change a form field's value, the view is likely the best place to do it. Validation methods should only be concerned with determining whether or not the current values are valid.
In the view, you are already assigning flow control depending on a bound form's validity - whether or not to redirect to a 'success' page, or redisplay the form. In many cases you are also pre-populating a form, as in the example of a form used to edit existing parameters. It seems a logical extension of this functionality to add extra control over a particular value.
精彩评论