Reputation: 605
I hope you guys could tell me how to use validation methods in my forms.py for a single field. My view is a generic.FormView
and my form is forms.Form
. Here is my view:
class ClassSetupView(OrganisorAndLoginRequiredMixin, generic.FormView):
template_name = "leads/class_setup.html"
form_class = ClassSetupForm
def get_success_url(self):
return reverse("class-setup-sucess-page")
def form_valid(self, form):
# Some Other Code
return super(ClassSetupView, self).form_valid(form)
Here is my form:
class ClassSetupForm(forms.Form):
date = forms.DateField(help_text="Year-01-01",)
clean_date(self):
date = self.cleaned_data['date']
if date < datetime.date.today():
raise ValidationError(_('Invalid date - renewal in past'))
return date
I hope you guys could tell me if it is okay to use the clean_date(self)
for the form as a way to validate the field date
. I saw several examples online, but I am not sure if clean_date(self)
will work for my case. Thank you.
Upvotes: 0
Views: 784
Reputation: 494
It's ok to use clean methods for validation. Check out the official django docs: https://docs.djangoproject.com/en/3.2/ref/forms/validation/#cleaning-a-specific-field-attribute
Upvotes: 1