In the end I came up with a solution which seems a bit hacky but does exactly what I want.
It looks like this:

def clean(self):
    cleaned_data = super(Form, self).clean()
check = cleaned_data.get("check", None)

    # ignore 'len' when 'check' is False
if not checkand "len" in self._errors:
        del self._errors["len"]

    return cleaned_data


one thing to note: "len" won't be available in cleaned_data but that's just cool since it's not valid :)


On 2012.07.26. 18:29, Kurtis Mullins wrote:
You could remove "required=True" from 'len' and override clean() to perform a multiple-field validation. (pretty much what Thomas mentioned)

len() will execute -- it will check to see if it's an integer. If you just want to completely ignore it, then do exactly as Thomas said, override. But you'll have to make sure you do the "Integer Validation" check in your clean() method if you ignore that validation in clean_len().

clean__len():
    return self.cleaned_data['len']

On Thu, Jul 26, 2012 at 12:21 PM, Zoltan Szalai <defaultd...@gmail.com <mailto:defaultd...@gmail.com>> wrote:

    On 2012.07.26. 17 <tel:2012.07.26.%2017>:44, Tomas Neme wrote:

            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.

        well, you could override clean_len() and not do anything in
        it, and
        then override clean() and do your check there.


    I don't think that would help. The clean method of the Field
    (IntegerField in this case) would still run.


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

Reply via email to