Sure Fire Way Your Forms Don't Validate

I saw this question being asked few different times.

“Why aren’t my forms validating?”.

When you do everything correctly, it can be really annoying if your forms don’t validate. All of their code looked perfect. I couldn’t figure out why the forms weren’t validating correctly. Maybe it’s a bug in Django? No…

I looked at the HTML of the form itself:

<form action="{% url 'poll:submit' %}" method="POST"> {% csrf_token %}
  <div class="form-group">
    <label for="comment">comment :</label>
    <textarea class="form-control" rows="3" id="comment"></textarea>
  <button type="submit" class="btn btn-success">SUBMIT</button>

Can you spot the issue with this form?

Before I get to the solution, this post requires a little bit more banter. I WANT to be able to solve this problem. I’ve been thinking about it for the last 2 - 3 weeks nonstop. If you do EVERYTHING correctly, but there seems that there’s NO solution, what do you do? How could you POSSIBLY figure out what is going wrong with your code?

I’m a professional developers and sometimes, I make the same mistakes as above because it’s SO DAMN easy to make these COMPLETELY simple mistakes.

But, what can you do about it? Here is a tiny bit of advice… Or, perhaps a heuristic for figuring out what you’re doing wrong…

  1. Find some code online that you can run that does something similar to what you’re trying to do (In this case, validating forms)
  2. Does the code run correctly? Run it using the same environment. (The same version of Python and the same version of Django)
  3. Now, copy and paste the code that works in your own application and alter the data so that it validates using your own forms.
  4. Does it work now?

With enough experience, you’ll be able to do this with your own code. (Over time, you’ll have plenty of code that you’ll know works.)

This is only ONE technique that I use as a professional programmer to fix bugs in my code.

Did you find the problem with the code above?

Maybe you’ve successfully wrote code that validates a form. Compare the code above with your code. What is different?

Give up? You need a name attribute in your input tags (that also includes textarea tags.)

Comment below if you already knew the answer!! And try my 4 step trick for figuring out how to troubleshoot your next application!