Re: hidden fields not cleaned
I'll try to answer myself :-) because widget=forms.HiddenInput() is invalid widget for DateTimeField. Changed in Django 1.0: The DateTimeField used to use a TextInput widget by default. This has now changed. So I left the original DateTimeInput for this field and used {{ form.some_hidden_field.as_hidden }} in the template It was almost correct, with a little help with http://code.djangoproject.com/ticket/9459 everything is ok. May be this discussion helps to someone ... On 23. Mar., 12:13 h., gentlestone wrote: > oh, great, you were right, thank you > > "Validation errors are hold back until the clean method returns the > cleaned_data dict." > > really the some_hidden_field was invalid, I included {{ form.errors }} > to my template and the validation error appeared suddenly :-) > > What I still don't understand, why this piece of code > > form = XyForm(initial = dict( > some_hidden_field = datetime.now(), > )) > > works with > forms.DateTimeField() > and doesn't work with > forms.DateTimeField(widget=forms.HiddenInput()) > > If the field is hidden, the initial value is invalid and is not > cleaned. Why? > > On 23. Mar., 10:57 h., Bjunix wrote: > > > > I thought my def clean(self) is the third step in your explanation: > > > Yes that's right. But this clean methods gets called even if your > > Field raised a ValidationError. Validation errors are hold back until > > the clean method returns the cleaned_data dict. Try to 'get' the key > > (not calling it directly) with cleaned_data.get() and let > > your clean method return cleaned_data and see if a ValidationError was > > raised earlier. Django did not put your hidden field into cleaned data > > probably because the hidden field value is just not clean. So, I dont > > think its a bug in django. > > > Maybe the hidden input has the wrong format or it's empty. Can you > > show the data you populate your hidden field with? > > > > 1. run the clean method of the form field itself. Here: > > > DateTimeField.clean() > > > 2. then run the clean_() method of the form if available > > > 3. once those two methods are run for every field, run the clean() > > > method of the form > > > > So I believe Django has already run: > > > 1. individual clean methods > > > 2. the () methods (actualy don't have any) > > > > and finally Django run MY OVERRIDED CLEAN method > > > > I expect my some_hidden_field should be already successfuly cleaned > > > (whitout ValidateError exception) and included in cleaned_data. > > > Appareantly no, because the key error. So this is propably a bug I > > > think. > > > > If I changed the problematic line > > > forms.DateTimeField(widget=forms.HiddenInput()) > > > to > > > forms.DateTimeField() > > > everything run whitout problems. Just my hidden field is'nt hidden. > > > This is the state of my application now. > > > > On 22. Mar., 17:35 h., Bjunix wrote: > > > > > Django validates form data in this order when calling is_valid or > > > > full_clean: > > > > > 1. run the clean method of the form field itself. Here: > > > > DateTimeField.clean() > > > > 2. then run the clean_() method of the form if available > > > > 3. once those two methods are run for every field, run the clean() > > > > method of the form > > > > > Source:http://docs.djangoproject.com/en/1.1/ref/forms/validation/#form-and-f... > > > > > So I guess the cleaned_data dict has no 'some_hidden_field' key. > > > > DateTimeField.clean() probably raised a Validation Error and did not > > > > populate the cleaned_data dictionary. > > > > > Try the following: > > > > > def clean(self): > > > > cleaned_data = self.cleaned_data > > > > #cleaned_data.get(key) returns None if key does not exist > > > > some_hidden_field = cleaned_data.get("some_hidden_field") > > > > > if some_hidden_field: > > > > #do your custom validation here and raise ValidationError > > > > if necessary > > > > # Always return the full collection of cleaned data. > > > > return cleaned_data > > > > > On Mar 22, 3:31 pm, gentlestone wrote: > > > > > > this piece of code leads to Key error 'some_hidden_field' > > > > > > class XyForm(Form): > > > > > > some_hidden_field = > > > > > forms.DateTimeField(widget=forms.HiddenInput()) > > > > > > def clean(self): > > > > > some_hidden_field = self.cleaned_data['some_hidden_field'] > > > > > > Why are hidden fields not cleaned? > > > > > I need to validate the hidden value and raise VadiationError if the > > > > > value is not ok. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: hidden fields not cleaned
oh, great, you were right, thank you "Validation errors are hold back until the clean method returns the cleaned_data dict." really the some_hidden_field was invalid, I included {{ form.errors }} to my template and the validation error appeared suddenly :-) What I still don't understand, why this piece of code form = XyForm(initial = dict( some_hidden_field = datetime.now(), )) works with forms.DateTimeField() and doesn't work with forms.DateTimeField(widget=forms.HiddenInput()) If the field is hidden, the initial value is invalid and is not cleaned. Why? On 23. Mar., 10:57 h., Bjunix wrote: > > I thought my def clean(self) is the third step in your explanation: > > Yes that's right. But this clean methods gets called even if your > Field raised a ValidationError. Validation errors are hold back until > the clean method returns the cleaned_data dict. Try to 'get' the key > (not calling it directly) with cleaned_data.get() and let > your clean method return cleaned_data and see if a ValidationError was > raised earlier. Django did not put your hidden field into cleaned data > probably because the hidden field value is just not clean. So, I dont > think its a bug in django. > > Maybe the hidden input has the wrong format or it's empty. Can you > show the data you populate your hidden field with? > > > 1. run the clean method of the form field itself. Here: > > DateTimeField.clean() > > 2. then run the clean_() method of the form if available > > 3. once those two methods are run for every field, run the clean() > > method of the form > > > So I believe Django has already run: > > 1. individual clean methods > > 2. the () methods (actualy don't have any) > > > and finally Django run MY OVERRIDED CLEAN method > > > I expect my some_hidden_field should be already successfuly cleaned > > (whitout ValidateError exception) and included in cleaned_data. > > Appareantly no, because the key error. So this is propably a bug I > > think. > > > If I changed the problematic line > > forms.DateTimeField(widget=forms.HiddenInput()) > > to > > forms.DateTimeField() > > everything run whitout problems. Just my hidden field is'nt hidden. > > This is the state of my application now. > > > On 22. Mar., 17:35 h., Bjunix wrote: > > > > Django validates form data in this order when calling is_valid or > > > full_clean: > > > > 1. run the clean method of the form field itself. Here: > > > DateTimeField.clean() > > > 2. then run the clean_() method of the form if available > > > 3. once those two methods are run for every field, run the clean() > > > method of the form > > > > Source:http://docs.djangoproject.com/en/1.1/ref/forms/validation/#form-and-f... > > > > So I guess the cleaned_data dict has no 'some_hidden_field' key. > > > DateTimeField.clean() probably raised a Validation Error and did not > > > populate the cleaned_data dictionary. > > > > Try the following: > > > > def clean(self): > > > cleaned_data = self.cleaned_data > > > #cleaned_data.get(key) returns None if key does not exist > > > some_hidden_field = cleaned_data.get("some_hidden_field") > > > > if some_hidden_field: > > > #do your custom validation here and raise ValidationError > > > if necessary > > > # Always return the full collection of cleaned data. > > > return cleaned_data > > > > On Mar 22, 3:31 pm, gentlestone wrote: > > > > > this piece of code leads to Key error 'some_hidden_field' > > > > > class XyForm(Form): > > > > > some_hidden_field = > > > > forms.DateTimeField(widget=forms.HiddenInput()) > > > > > def clean(self): > > > > some_hidden_field = self.cleaned_data['some_hidden_field'] > > > > > Why are hidden fields not cleaned? > > > > I need to validate the hidden value and raise VadiationError if the > > > > value is not ok. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: hidden fields not cleaned
> I thought my def clean(self) is the third step in your explanation: Yes that's right. But this clean methods gets called even if your Field raised a ValidationError. Validation errors are hold back until the clean method returns the cleaned_data dict. Try to 'get' the key (not calling it directly) with cleaned_data.get() and let your clean method return cleaned_data and see if a ValidationError was raised earlier. Django did not put your hidden field into cleaned data probably because the hidden field value is just not clean. So, I dont think its a bug in django. Maybe the hidden input has the wrong format or it's empty. Can you show the data you populate your hidden field with? > 1. run the clean method of the form field itself. Here: > DateTimeField.clean() > 2. then run the clean_() method of the form if available > 3. once those two methods are run for every field, run the clean() > method of the form > > So I believe Django has already run: > 1. individual clean methods > 2. the () methods (actualy don't have any) > > and finally Django run MY OVERRIDED CLEAN method > > I expect my some_hidden_field should be already successfuly cleaned > (whitout ValidateError exception) and included in cleaned_data. > Appareantly no, because the key error. So this is propably a bug I > think. > > If I changed the problematic line > forms.DateTimeField(widget=forms.HiddenInput()) > to > forms.DateTimeField() > everything run whitout problems. Just my hidden field is'nt hidden. > This is the state of my application now. > > On 22. Mar., 17:35 h., Bjunix wrote: > > > Django validates form data in this order when calling is_valid or > > full_clean: > > > 1. run the clean method of the form field itself. Here: > > DateTimeField.clean() > > 2. then run the clean_() method of the form if available > > 3. once those two methods are run for every field, run the clean() > > method of the form > > > Source:http://docs.djangoproject.com/en/1.1/ref/forms/validation/#form-and-f... > > > So I guess the cleaned_data dict has no 'some_hidden_field' key. > > DateTimeField.clean() probably raised a Validation Error and did not > > populate the cleaned_data dictionary. > > > Try the following: > > > def clean(self): > > cleaned_data = self.cleaned_data > > #cleaned_data.get(key) returns None if key does not exist > > some_hidden_field = cleaned_data.get("some_hidden_field") > > > if some_hidden_field: > > #do your custom validation here and raise ValidationError > > if necessary > > # Always return the full collection of cleaned data. > > return cleaned_data > > > On Mar 22, 3:31 pm, gentlestone wrote: > > > > this piece of code leads to Key error 'some_hidden_field' > > > > class XyForm(Form): > > > > some_hidden_field = > > > forms.DateTimeField(widget=forms.HiddenInput()) > > > > def clean(self): > > > some_hidden_field = self.cleaned_data['some_hidden_field'] > > > > Why are hidden fields not cleaned? > > > I need to validate the hidden value and raise VadiationError if the > > > value is not ok. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: hidden fields not cleaned
I thought my def clean(self) is the third step in your explanation: 1. run the clean method of the form field itself. Here: DateTimeField.clean() 2. then run the clean_() method of the form if available 3. once those two methods are run for every field, run the clean() method of the form So I believe Django has already run: 1. individual clean methods 2. the () methods (actualy don't have any) and finally Django run MY OVERRIDED CLEAN method I expect my some_hidden_field should be already successfuly cleaned (whitout ValidateError exception) and included in cleaned_data. Appareantly no, because the key error. So this is propably a bug I think. If I changed the problematic line forms.DateTimeField(widget=forms.HiddenInput()) to forms.DateTimeField() everything run whitout problems. Just my hidden field is'nt hidden. This is the state of my application now. On 22. Mar., 17:35 h., Bjunix wrote: > Django validates form data in this order when calling is_valid or > full_clean: > > 1. run the clean method of the form field itself. Here: > DateTimeField.clean() > 2. then run the clean_() method of the form if available > 3. once those two methods are run for every field, run the clean() > method of the form > > Source:http://docs.djangoproject.com/en/1.1/ref/forms/validation/#form-and-f... > > So I guess the cleaned_data dict has no 'some_hidden_field' key. > DateTimeField.clean() probably raised a Validation Error and did not > populate the cleaned_data dictionary. > > Try the following: > > def clean(self): > cleaned_data = self.cleaned_data > #cleaned_data.get(key) returns None if key does not exist > some_hidden_field = cleaned_data.get("some_hidden_field") > > if some_hidden_field: > #do your custom validation here and raise ValidationError > if necessary > # Always return the full collection of cleaned data. > return cleaned_data > > On Mar 22, 3:31 pm, gentlestone wrote: > > > this piece of code leads to Key error 'some_hidden_field' > > > class XyForm(Form): > > > some_hidden_field = > > forms.DateTimeField(widget=forms.HiddenInput()) > > > def clean(self): > > some_hidden_field = self.cleaned_data['some_hidden_field'] > > > Why are hidden fields not cleaned? > > I need to validate the hidden value and raise VadiationError if the > > value is not ok. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: hidden fields not cleaned
Django validates form data in this order when calling is_valid or full_clean: 1. run the clean method of the form field itself. Here: DateTimeField.clean() 2. then run the clean_() method of the form if available 3. once those two methods are run for every field, run the clean() method of the form Source: http://docs.djangoproject.com/en/1.1/ref/forms/validation/#form-and-field-validation So I guess the cleaned_data dict has no 'some_hidden_field' key. DateTimeField.clean() probably raised a Validation Error and did not populate the cleaned_data dictionary. Try the following: def clean(self): cleaned_data = self.cleaned_data #cleaned_data.get(key) returns None if key does not exist some_hidden_field = cleaned_data.get("some_hidden_field") if some_hidden_field: #do your custom validation here and raise ValidationError if necessary # Always return the full collection of cleaned data. return cleaned_data On Mar 22, 3:31 pm, gentlestone wrote: > this piece of code leads to Key error 'some_hidden_field' > > class XyForm(Form): > > some_hidden_field = > forms.DateTimeField(widget=forms.HiddenInput()) > > def clean(self): > some_hidden_field = self.cleaned_data['some_hidden_field'] > > Why are hidden fields not cleaned? > I need to validate the hidden value and raise VadiationError if the > value is not ok. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
hidden fields not cleaned
this piece of code leads to Key error 'some_hidden_field' class XyForm(Form): some_hidden_field = forms.DateTimeField(widget=forms.HiddenInput()) def clean(self): some_hidden_field = self.cleaned_data['some_hidden_field'] Why are hidden fields not cleaned? I need to validate the hidden value and raise VadiationError if the value is not ok. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.