Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my custom validation not being called?
    primarykey
    data
    text
    <p>I have this model and modelform:</p> <pre><code>class Comment(models.Model): text = models.CharField(max_length=100) def clean_text(self): print "called Comment" if len(self.text) &lt;= 5: raise ValidationError("must have 5 chars at least") class CommentForm(ModelForm): class Meta: model = Comment def clean_text(self): print "called CommentForm" if len(self.text) &lt;= 5: raise ValidationError("must have 5 chars at least") </code></pre> <p>And I'm using them like this in the view:</p> <pre><code> CommentFormSet = modelformset_factory(model=Comment,form=CommentForm,extra=3) if request.method == "POST": formset = CommentFormSet(request.POST) if formset.is_valid(): print "VALID FORM" else: formset = CommentFormSet() return render_to_response("first.html",{"formset":formset},context_instance=RequestContext(request)) </code></pre> <p>And this is my template:</p> <pre><code>&lt;form action="first" method="post"&gt; {% csrf_token %} {% for dict in formset.errors %} {% for error in dict.values %} {{ error }} {% endfor %} {% endfor %} &lt;table&gt; {{ formset }} &lt;/table&gt; &lt;input type="submit" value="Create"/&gt; &lt;/form&gt; </code></pre> <p>The thing is, my validation is never called. I have 3 comments which I can add at once, and if their text field is empty, django says it's no problem. What am I not doing right?</p> <p>EDIT: The variant with validator:</p> <pre><code>def validate_min(val): print "validator called" if len(val) &lt;= 5: raise ValidationError("At least 5 characters should be provided") class Comment(models.Model): text = models.CharField(max_length=100,validators=[validate_min]) </code></pre> <p>My validator is not being called.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload