Reputation: 723
I'm trying to validate a form, user can't send contact info through the form, to achieve that, I have:
class ActualizarCotizacionForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ActualizarCotizacionForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
self.fields['proposal'].widget.attrs['rows'] = '4'
self.fields['deliverable'].widget.attrs['rows'] = '4'
class Meta():
model = AffiliateQuote
fields = ['proposal','deliverable','delivery_time','fee']
def clean(self):
data = self.cleaned_data['proposal']
if "311" in data:
raise forms.ValidationError("You cant send phone numbers through the form")
return data
And in template I have this line above the field proposal
: {{form.proposal.errors.as_text}}
Validation "311" that I had put here it's just for example, but with this, I cant get put it works, I write "311" in proposal
field and the error does not display, how can I achieve this?
Upvotes: 0
Views: 1640
Reputation: 51978
Well put this validation in clean_proposal
method, for example:
def clean_proposal(self):
data = self.cleaned_data['proposal']
if "311" in data:
raise forms.ValidationError("You cant send phone numbers through the form")
return data
Upvotes: 1