title: Django inlineformset_factory validators not working
body: <h2>Code Snippet</h2>
  <p>Django version: 3.1.7</p>
  <p>models.py</p>
  <pre><code># custom validator
  is_numeric = RegexValidator(r&quot;^[0-9]*$&quot;, &quot;Only numbers are allowed&quot;)
  
  class Person(models.Model):
      # all the boring fields
  
  
  class PersonAddress(models.Model):
      # all the boring fields
      person = models.ForeignKey(
          Person, null=True, on_delete=models.SET_NULL, related_name=&quot;person_address&quot;,
      )
      postcode = models.CharField(
          blank=True, help_text=&quot;Include leading zero if exists&quot;,
          max_length=10, validators=[is_numeric],
      )
  </code></pre>
  <p><code>PersonAddress</code> will be the inlineformset of <code>Person</code> as a Person can have multiple addresses</p>
  <p>forms.py</p>
  <pre><code>class PersonForm(forms.ModelForm):
      class Meta:
          model = Person
          fields = &quot;__all__&quot;
  
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          self.helper = FormHelper()
  
  
  class PersonAddressForm(forms.ModelForm):
      class Meta:
          model = PersonAddress
          fields = &quot;__all__&quot;
  
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          self.helper = FormHelper()
          self.helper.form_method = &quot;post&quot;
  
  
  PersonAddressFormSet = forms.models.inlineformset_factory(
      Person, PersonAddress, form=PersonAddressForm, extra=1, can_delete=True,
  )
  
  # min_num, validate_min don't help even though I read from other stackoverflow solution
  # PersonAddressFormSet = forms.models.inlineformset_factory(
  #     Person, PersonAddress, form=PersonAddressForm, extra=1, can_delete=True, min_num=1, validate_min=True,
  # )
  </code></pre>
  <p>views.py</p>
  <pre><code># Person CreateView
  class PersonCreateView(SuccessMessageMixin, LoginRequiredMixin, CreateView):
      model = Person
      form_class = PersonForm
  
      def get_context_data(self, **kwargs):
          context = super(PersonCreateView, self).get_context_data(**kwargs)
          if self.request.POST:
              context[&quot;addresses&quot;] = PersonAddressFormSet(self.request.POST, self.request.FILES)
          else:
              context[&quot;addresses&quot;] = PersonAddressFormSet()
          return context
  
      # I think the problem is here, not 100% sure
      def form_valid(self, form):
          context = self.get_context_data()
          addresses = context[&quot;addresses&quot;]
          self.object = form.save()
          if addresses.is_valid():
              addresses.instance = self.object
              addresses.save()
  
          # https://stackoverflow.com/questions/47476941/django-inlineformset-factory-error-messages-not-working
          # my attempt, got this error
          # TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
          # else:
          #    return render(self.request, self.template_name, context)
  
          return super(PersonCreateView, self).form_valid(form)
  
  
  # PersonAddress CreateView
  class PersonAddressCreateView(SuccessMessageMixin, LoginRequiredMixin, CreateView):
      model = PersonAddress
      form_class = PersonAddressForm
  </code></pre>
  <h2>Problem</h2>
  <p>If I create a <code>PersonAddress</code> object with <code>postcode</code> that's non-numeric string, the <code>PersonAddressCreateView</code> HTML form will return validation error.</p>
  <p>But if I do the same thing in <code>PersonCreateView</code> HTML form, there won't be any validation error. <code>Person</code> object will be created, <code>PersonAddress</code> won't be created as the <code>postcode</code> is invalid. What I am expecting is for the <code>PersonCreateView</code> HTML form to show me the validation error instead of saving the form.</p>
  <p>I think the solution should have something to do with the <code>PersonCreateView</code>'s <code>form_valid</code>, but I am not sure.</p>
  <p>Thanks in advance!</p>