Sunday, June 1, 2014

Re: InlineFormSet validation

Hi Cesar,

It's all the documentation:


You just have to raise a ValidationError(); Django will then add an error that spans the entire formset. 

If you want to evaluate errors in clean(), but force the actual error to appear against a specific field, that's possible too - you just need to emulate what Django is doing behind the scenes. That means inserting the error into the error dictionary, and removing the error value from cleaned_data:

    self._errors['field name'] = self.error_class(['This field has a problems'])
    del cleaned_data['field_name']

You might need to do this if you won't know if a specific value is valid until you've checked all the individual values (e.g., A must be greater than B - you can't check that until you know both A and B exist, which won't be the case in the clean_A and clean_B methods)

I hope that helps!

Yours,
Russ Magee %-)



On Sun, Jun 1, 2014 at 11:52 PM, César García Tapia <tapia@openshine.com> wrote:
Hi. I already asked this question in StackOverflow, but I didn't get any useful answer. Let's try here :-)

I have a BaseInlineFormSet, and I'd like to validate a field in the parent form based on the values on the fields of the children. As seen in the docs, the only method to make a custom validation is clean(), but I can't find a way to add errors to the parent form, only for the children.

In the following code I build a formula. Each variable comes from a inner form, and if the global formula don't validate, I'd like to add an error to the parent's form field formula

class CustomInlineFormSet(BaseInlineFormSet):
    def clean(self):
        formula = self.instance.formula
        variables = []
        for form in self.forms:
            if 'formula_variable' in form.cleaned_data:
                variables.append(form.cleaned_data['formula_variable'])

        valid, msg = validate_formula(formula, variables)
        if not valid:
            WHAT HERE???

Thank you!!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87ce32a8-782e-4248-8b8c-046b6eef0904%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJxq849ckBkXM9Wbfb%2B%3DAnu5BALoN6FWT7%3DGyTDHqvp8u%2BT%3Duw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment