What is the "general way" to add your own validation to forms under
the newforms framework?

Subclassing and adding in your clean_* methods? Using decorators or
the such around existing validation? Adding your own custom Field
objects and setting their "clean" methods?

-joe

On 5/21/07, Matt <[EMAIL PROTECTED]> wrote:
>
> I have a form that allows users to submit their name and email address
> for an invitation.
> I do not want people signing up more than once. The following code
> works, but it does not seem like the "Best Practice" solution.
>
> forms.py
> [...imports...]
> class RequestInvite(forms.Form):
>         name = forms.CharField(max_length=75)
>         email = forms.EmailField()
>
>         def clean_name(self):
>                 try:
>                         u = UserInvite.objects.get(name__contains =
> self.clean_data['name'])
>                         raise ValidationError('You have already signed up.')
>                 except UserInvite.DoesNotExist:
>                         return self.clean_data['name']
>
>         def clean_email(self):
>                 try:
>                         u = UserInvite.objects.get(email__contains =
> self.clean_data['email'])
>                         raise ValidationError('You have already signed up.')
>                 except UserInvite.DoesNotExist:
>                         return self.clean_data['email']
>
>         def save(self):
>                 invite = UserInvite(
>                         name = self.clean_data['name'],
>                         email = self.clean_data['email'],)
>                 invite.save()
>
> views.py
> [...imports...]
> def request_invite(request):
>
>         f = RequestInvite()
>
>         if request.method == 'POST':
>                 invite_me = RequestInvite(request.POST)
>                 if invite_me.is_valid():
>                         invite_me.save()
>                         msg = "We will be sending out invitations shortly."
>                         return render_to_response( 'invite/thanks.html',
>                                 {'msg': msg})
>                 else:
>                         f = invite_me
>
>         return render_to_response( 'invite/index.html',
>                 {'form':f,})
>
> If someone could point me in the right direction it would be greatly
> appreciated.
> Thanks Matt
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to