0
votes

My Flask form is not getting validated. Tried everything. form.validate() is not validating, although, there is no validation present. Is there any way out of this situation?

HTML Code

<body>
    <form method="POST" action = "{{ url_for('/generate') }}" novalidate>
        {{ form.csrf_token }}
        {{ form.hidden_tag() }} <br>
        {{ form.IsAssistantComm() }} <br>
        {{ form.Submit() }} <br>
    </form>
</body>

Flask Code

class GeneratorForm(FlaskForm):
    To = StringField('TO')
    IsAssistantComm = BooleanField('Is Assistant Commissioner?')
    Submit = SubmitField('Generate')

@app.route('/generate', methods=['GET', 'POST'])
def generate():
    form = GeneratorForm()
    if form.is_submitted():
        print("submitted")
    if form.validate():
        print("valid")
    if form.validate_on_submit():
        print("I am here")
        IsAssistantComm = form.IsAssistantComm.data
        return redirect('/')
    return render_template('main.html', form=form)

Program Stacktrace:


 * Running on http://127.0.0.1:5090/ (Press CTRL+C to quit)
submitted
127.0.0.1 - - [30/May/2020 18:48:08] "POST /generate HTTP/1.1" 200 -
submitted
127.0.0.1 - - [30/May/2020 18:48:09] "POST /generate HTTP/1.1" 200 -

1
Could you add the output of print(form.errors), it would help with debugging.Atte
Also, I don't think you need both {{ form.csrf_token }} and {{ form.hidden_tag() }} in your template file, hidden tag should include the CSRF token.Atte
Does this answer your question? WTF form.validate_on_submit() not workingrfkortekaas

1 Answers

0
votes

I might be mistaken here, but I think it just might be the fact that you have no form validation in your GenerateForm class, do something like this :

class GeneratorForm(FlaskForm):
To = StringField('TO')
IsAssistantComm = BooleanField('Is Assistant Commissioner?', validators =[DataRequired()])
Submit = SubmitField('Generate')

Let me know if that fixes it :D