[EMAIL PROTECTED] wrote:
> 
> 
> On Nov 7, 1:53 pm, Jonathan Buchanan <[EMAIL PROTECTED]>
> wrote:
>> [EMAIL PROTECTED] wrote:
>>> Here's what I've done:
>>> if request.method == 'POST':
>>>         submission = request.POST.copy()
>>>         submission['slug'] = slugify(submission['name'])
>>>         form = ClubFormClass(submission)
>>>         if Club.objects.get(slug=submission['slug']):
>>>           form.errors['name'] = '<ul><li>Club is already in database</
>>> li></ul>'
>>>         if form.is_valid():
>>>                    form.save()
>>>                    return HttpResponseRedirect('/thankyou/')
>>>     else:
>>>         form = ClubFormClass()
>> I'd implement it as a clean_fieldname method. The fact that in the above
>> you're having to mess around with errors dictionary manually for
>> something this simple is a bad smell to watch out for.
>>
>> form
>> ----
>>
>> def ClubForm(forms.Form):
>>      name = forms.CharField(...)
>>      ...
>>
>>      def clean_name(self):
>>         slug = slugify(self.cleaned_data['name'])
>>          if Club.objects.filter(slug=slug).count():
>>              raise forms.ValidationError(u'Club is already in database')
>>         return self.cleaned_data['name']
>>
>> view
>> ----
>>
>> if request.method == 'POST':
>>      form = ClubForm(data=request.POST)
>>      if form.is_valid():
>>          club = form.save() # Do the slugify() in your save method
>>          return HttpResponseRedirect(club.get_absolute_url())
>> else:
>>      form = ClubForm()
>>
>> Jonathan.
> 
> Thanks Jonathan... yeah, I smelled that smell. I'm not quite following
> you, though. Where is clean_name() called? I think I may be confused
> because I have     ClubFormClass = nforms.form_for_model(Club)  rather
> than def ClubForm(forms.Form):

Any clean_FIELDNAME methods are called as part of the validation 
triggered when you call is_valid [1]. I assumed you were creating a 
custom form before - in your case, you'll want to use the 'form' 
argument [2] to pass in a BaseForm which implements any extra 
functionality you need:

forms
-----
class ClubBaseForm(forms.BaseForm):
     def clean_name(self):
         slug = slugify(self.cleaned_data['name'])
         if Club.objects.filter(slug=slug).count():
             raise forms.ValidationError(u'Club is already in database')
         return self.cleaned_data['name']

view
----
ClubForm = form_for_model(Club, form=ClubBaseForm)
if request.method == 'POST':
      form = ClubForm(data=request.POST)
      if form.is_valid():
          # For DRY's sake, I'm assuming the slug is set by an
          # overriden save() method in the Club model.
          club = form.save()
          return HttpResponseRedirect(club.get_absolute_url())
else:
      form = ClubForm()

I've found the trick with newforms is that if you can get the form to do 
as much work as possible for you validation-wise, displaying and 
redisplaying with errors is a piece of cake, no matter how complex the form.

Jonathan.

[1] 
http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation
[2] 
http://www.djangoproject.com/documentation/newforms/#using-an-alternate-base-class

--~--~---------~--~----~------------~-------~--~----~
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