#18988: initialization of hidden_initial_fields after a form was bound
-------------------------+-------------------------------------------------
     Reporter:  its@…    |      Owner:  nobody
         Type:           |     Status:  new
  Uncategorized          |    Version:  1.2
    Component:  Forms    |   Keywords:  forms, show_hidden_initial,
     Severity:  Normal   |  change_data
 Triage Stage:           |  Has patch:  0
  Unreviewed             |      UI/UX:  0
Easy pickings:  0        |
-------------------------+-------------------------------------------------
 After a failed form validation this forms hidden_initial-fields (rendered
 via the field argument show_hidden_initial=True) will be rendered
 containing the submitted data of the corresponding regular fields not the
 data the hidden_initial_fields have submitted

 This might cause confusion because: if the form was not saved the value
 was not saved in the database but is now considered as current db-state in
 the hidden_initial_field
 If a succesive submit is triggered the form will now not identify a form
 .has_changed-event for the corresponding field

 IMHO the hidden_initial_field should recieve its POST/GET data from the
 hidden_initial_field-data instead of the POST/GET data from the original
 field after a submit



 --how to reproduce
 -field ('label') has the argument show_hidden_initial=True
 -open the view with an instance from the database
 -change the value of the label field and save -> form will be declared
 invalid and form.label_initial will contain the new value despite that the
 form could not be saved
 -in addition if save is clicked again the form is valid and
 form.changed_data will be empty


 This problem occurred in version 1.2 but considering the implementation of
 BoundField this will also happen in version 1.4


 {{{



 class MyModel(models.Model)
     label = models.CharField()


 class MyModelForm(forms.Form)
     def __init__(*args, **kwargs):
         super(MyModelForm, self).__init__(*args, **kwargs)
         for f in self.fields.keys():
             self.fields[f].show_hidden_initial = True

     class Meta:
         model = MyModel

     def clean(self):

         cleaned_data = super(MyModelForm, self).clean()

         if self.changed_data:
             self._errors['label'] = ErrorList(['form has changed'])

         return cleaned_data


 def myView(request, modelId=None):

     if modelId:
         instance = MyModel.objects.get(pk=modelId)
     else:
         instance = None

     if request.method == 'POST':
         form = MyModelForm(request.POST, instance=instance)

         if form.is_valid():
             instance = form.save()
             return HttpResponse('Success')
     else:
         form = MyModelForm(instance=instance)

     template = '<FORM method="POST" enctype="application/x-www-form-
 urlencoded"><table>{{form}}</table><input method="POST" name="submit"
 type="submit" value="Save"\></Form>'

     t = template.Template(output)
     c = template.Context({'form':form})
     return t.render(c)


 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18988>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to