Hi, Russell, thanks for answering :-)

Your answer helps me, but not completely. I can add the error with this:

    self._errors.append({'formula': self.error_class([msg])})  # 
self._errors in a BaseInlineFormSet is a list of dictionaries, not a 
dictionary, like you say.

But I don't know how to reach the cleaned_data of the parent form. If I do 
this:

    del self.cleaned_data['formula']

It says that the parent form doesn't have a 'cleaned_data' attribute.

So I can make the InlineFormSet validation fail, but I can't show the error 
in my template.

Any suggestion?

Thank you!!






El lunes, 2 de junio de 2014 02:26:56 UTC+2, Russell Keith-Magee escribió:
>
> Hi Cesar,
>
> It's all the documentation:
>
>
> https://docs.djangoproject.com/en/dev/topics/forms/formsets/#custom-formset-validation
>
> 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 <[email protected] 
> <javascript:>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> 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
>>  
>> <https://groups.google.com/d/msgid/django-users/87ce32a8-782e-4248-8b8c-046b6eef0904%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> 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 [email protected].
To post to this group, send email to [email protected].
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/27bc6913-24c6-4de9-8f9d-99f2bdeafb3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to