Friday, July 27, 2012

Re: ignore field during form validation

On Thu, Jul 26, 2012 at 11:14 AM, Zoltan Szalai <defaultdict@gmail.com> wrote:
Hi all,

Let's assume i have to the following simple form:


class Form(forms.Form):

    check = forms.BooleanField(
        required=False,
    )
    # take into account only when 'check' is True
    len = forms.IntegerField(
        min_value=3,
        max_value=5,
        required=True,
    )


What I want is to validate the 'len' field only when 'check' is True.
I could define the clean method of the form and validate the required,
min_value and max_value stuff only when 'check' is True but the case when
someone types a non integer value into the input is still there. How could I skip that? That check is done by the IntegerField.

You could use two different Django forms, and only call is_valid() on the one with the len value if the first one with the check value indicates that check is True. Both of these Django forms can be rendered in a single HTML <form>. I think that approach is a little cleaner than inspecting the POST data yourself.

Karen
--
http://tracey.org/kmt/

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

No comments:

Post a Comment