Reputation: 197
working on django forms here and i've trouble with errors. I know how to detect them but i cannot (for some reasons) print an error under a form when is is uncorrect.
The class :
class descclient_form(forms.Form):
TYPE_CHOICE = (
('C', ('Client')),
('F', ('Facture')),
('V', ('Visite'))
)
file_type = forms.ChoiceField(choices = TYPE_CHOICE, widget=forms.RadioSelect)
file_name = forms.CharField(max_length=200)
file_cols = forms.CharField(max_length=200, widget=forms.Textarea)
# file_date = forms.DateField()
file_sep = forms.CharField(max_length=5, initial=';')
file_header = forms.CharField(max_length=200, initial='0')
# Check if file_cols is correctly filled
def clean_cols(self):
cleaned_file_type = self.cleaned_data.get("file_type")
cleaned_file_cols = self.cleaned_data.get("file_cols")
if cleaned_file_type == 'C':
if 'client' not in cleaned_file_cols:
self._errors['file_cols'] = [u'Requiers \'Client\' in collumn selection']
print 'error'
if cleaned_file_type == 'F':
mandatory_field = ('fact', 'caht', 'fact_dat')
for mf in mandatory_field:
if mf not in cleaned_file_cols:
self._errors["file_cols"] = self.error_class(['Requiers \'fact\', \'caht\' \'fact_dat\ in file_cols'])
print 'error'
return self.cleaned_data
Here is the function called :
def descclient(request):
if request.method == 'POST':
form = descclient_form(data=request.POST)
if form.is_valid():
form.clean_cols()
return render_to_response('descclient.html', {'form': descclient_form}, context_instance=RequestContext(request))
And Template :
<form action="/descclient" method="post">
<div class="fieldWrapper">
{{ form.file_type_errors }}
<label for="id_subject">File type:</label>
{{ form.file_type }}
</div>
<div class="fieldWrapper">
{{ form.file_name_errors }}
<label for="id_subject">File name:</label>
{{ form.file_name }}
</div>
<div class="fieldWrapper">
{{ form.file_cols_errors }}
<label for="id_subject">Collumns:</label>
{{ form.file_cols }}
</div>
<div class="fieldWrapper">
{{ form.file_sep_errors }}
<label for="id_subject">Separator:</label>
{{ form.file_sep }}
</div>
<div class="fieldWrapper">
{{ form.file_header_errors }}
<label for="id_subject">Header:</label>
{{ form.file_header }}
</div>
{% csrf_token %}
<p><input type="submit" value="Validate"></p>
For some reaons the "{{ form.file_cols_errors }}" doesn't print anything
EDIT :
So, as pointed i changed the clean_cols method to clean. I must be some kind of lost because it doesn't even get into the if statement
def clean(self):
cleaned_data = super(descclient_form, self).clean()
cleaned_file_type = cleaned_data("file_type")
cleaned_file_cols = cleaned_data("file_cols")
if cleaned_file_type == 'C':
if 'client' not in cleaned_file_cols:
raise forms.ValidationError("This field throws an error")
if cleaned_file_type == 'F':
mandatory_field = ('fact', 'caht', 'fact_dat')
for mf in mandatory_field:
if mf not in cleaned_file_cols:
raise forms.ValidationError("This field throws an error")
return cleaned_data
Upvotes: 0
Views: 884
Reputation: 3208
Instead of clean_cols
that you call after is_valid()
you should implement clean
method and do these validations there.
See https://docs.djangoproject.com/en/1.4/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other for more details.
Upvotes: 2
Reputation: 10847
You should raise a ValidationError in a "clean_file_cols" method:
raise forms.ValidationError("This field throws an error")
For details, have a look at the documentation. And btw: You should avoid using "print" anywhere in your django project, because on most webserver configuration, it leads to a server error. Use logging instead.
Upvotes: 0