Saturday, January 26, 2013

Form with variable fields

Hey there,

I'm trying to create a form with a "static" part (a "title" field, a "date" field) and a variable part (these are "artist names", so that's a single field that can be repeated multiple times, with a minimum of 1). I first thought about the MultiValueField but it requires several fields, and in my case I only want to use 1 field, just like an inline with one field in the Django admin. I then tried to go with a Formset, but I wasn't able to set it as mandatory (even if the field is marked as mandatory, if it's left blank it's just like the form is not filled so the validation always passes). Here's the code I tried for the formset part:

class ArtistForm(forms.Form):      name = forms.CharField(max_length=255, required=True)    ArtistFormSet = formset_factory(ArtistForm)  af = ArtistFormSet({      'form-0-name': '',      'form-TOTAL_FORMS': u'1',      'form-INITIAL_FORMS': u'0',      'form-MAX_NUM_FORMS': u''  })    assert False, af.is_valid()

In this case, is_valid() is always True. However if I a second field to my form, that is required as well, and that only one of the two fields is filled, the validation doesn't pass anymore.

I also thought of not using a form at all, but I should then check manually if at least 1 "artist name" has been filled, and manage the errors manually.

I guess some people already implemented some "custom inlines" for the frontend part of their websites. How did you manage to do that (with or without Django forms)?

Thanks!

--
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 django-users+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment