Try using

self.add_error('state': 'Must select state if province is not entered.')

or
raise ValidationError({'state': 'Must select state if province is not 
entered.'})


From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Andrew Michael
Sent: Tuesday, April 25, 2017 2:29 PM
To: Django users
Subject: Can't get error to display when using inlineformset_factory.

I am trying to get validation errors to appear on my web page when using an 
inlineformset_factory. I have a main form called PersonalInformation form and 3 
inline forms (Relative, PositionHeld and Phone). I am receiving the main form 
errors on my page, but I can't get the 3 inline forms to display the errors.

Do I need to write custom errors?

Can anyone help? I have searched on Google and tried several different things 
with no luck.

Thanks.

#form.py

class PersonalInformationForm(forms.ModelForm):
    class Meta(object):
        model = Applicant
        fields = ('position', 'salutation', 'first_name', 'middle_name', 
'last_name', 'suffix',
                  'address1', 'address2', 'city', 'state', 'zip_code', 
'province', 'country', 'origin_country',
                  'is_us_armed_forces', 'armed_forces_branch', 
'is_relative_of_pcusa',
                  'is_previous_employee', 'is_legal_work_in_us',)

    def __init__(self, *args, **kwargs):
        super(PersonalInformationForm, self).__init__(*args, **kwargs)
        self.fields['address1'].required = True
        self.fields['city'].required = True
        self.fields['zip_code'].required = True
        self.fields['country'].required = True
        self.fields['origin_country'].required = True
        self.fields['is_legal_work_in_us'].required = True

    def clean(self):
        if 'province' in self.cleaned_data and 
self.cleaned_data.get('province') == '':
            if not self.cleaned_data.get('state'):
                self._errors['state'] = ErrorList(['Must select state if 
province is not entered.'])
        if self.cleaned_data.get('is_us_armed_forces'):
            if self.cleaned_data.get('armed_forces_branch') == '':
                self._errors['armed_forces_branch'] = ErrorList(['You must 
enter branch if you checked that you served in the U.S. Armed Forces.'])
        return self.cleaned_data


#views.py

@login_required
def personal_information(request, expected_applicant_id, position_id, 
private_code):
    expected_applicant = get_object_or_404(ExpectedApplicant, \
        pk=expected_applicant_id, position_id=position_id, 
private_code=private_code)

    applicant = None
    applicant_qs = Applicant.objects.filter(user=request.user, 
position=expected_applicant.position)
    if applicant_qs.count() > 0:
        applicant = applicant_qs[0]
    else:
        applicant = Applicant(user=request.user, 
position=expected_applicant.position, status = APPLICATION_STATUS_INCOMPLETE)

    RelativeInlineFormSet = inlineformset_factory(Applicant, Relative, 
can_delete=False, extra=1)
    PositionHeldInlineFormSet = inlineformset_factory(Applicant, PositionsHeld, 
can_delete=False, extra=1)
    PhoneInlineFormSet = inlineformset_factory(Applicant, Phone, 
can_delete=False, extra=2)
    applicant.salutation = expected_applicant.salutation
    applicant.suffix = expected_applicant.suffix
    applicant.first_name = expected_applicant.first_name
    applicant.middle_name = expected_applicant.middle_name
    applicant.last_name = expected_applicant.last_name
    applicant.state = expected_applicant.state
    applicant.city = expected_applicant.city
    applicant.save()
    form = PersonalInformationForm(instance=applicant)
    formset1 = RelativeInlineFormSet(instance=applicant)
    formset2 = PositionHeldInlineFormSet(instance=applicant)
    formset3 = PhoneInlineFormSet(instance=applicant)
    position_title = expected_applicant.position.position_title
    complete_name = expected_applicant.get_complete_name()

    if request.POST:
        form = PersonalInformationForm(request.POST, request.FILES, 
instance=applicant)
        if form.is_valid():
            applicant = form.save(commit=False)
            applicant.save()
            phone_formset = PhoneInlineFormSet(request.POST, instance=applicant)
            relative_formset = RelativeInlineFormSet(request.POST, 
instance=applicant)
            position_formset = PositionHeldInlineFormSet(request.POST, 
instance=applicant)
            if phone_formset.is_valid() and relative_formset.is_valid() and 
position_formset.is_valid():
                phone_formset.save()
                relative_formset.save()
                position_formset.save()
        context = {
        'form': form,
        'formset1': formset1,
        'formset2': formset2,
        'formset3': formset3,
        'position_title': position_title,
        'complete_name': complete_name,
    }
    return render_to_response('applications/personal_information.html', context,
            context_instance=RequestContext(request))

--
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+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0146c435-66ee-4c74-8bc3-fad890675bbb%40googlegroups.com<https://groups.google.com/d/msgid/django-users/0146c435-66ee-4c74-8bc3-fad890675bbb%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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/27ea3dcb676d471c808f302c8ff4254c%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to