Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

python - validate_on_submit() from Flask-WTF always returns true

I'm using Flask-WTF's validate_on_submit() function for very simple one-page website in a such code:

class SignForm(FlaskForm):
    testStrField = StringField("enter some text data")
    doCheckSign = SubmitField('Check the signed data')

@AppEDS1.route('/', methods=['GET', 'POST'])
def index1():
    form = SignForm()
    if form.validate_on_submit():
        ....

In my <form></form> there are also some buttons with separate JS-handlers for onclick events, and I noticed that each POST-request to my site triggers validate_on_submit() in the way it returns TRUE for each request. Actually, the only difference between requests is in corresponding Request Payload sections:

(for the "just-onclick-POST-request") csrf_token=blablabla&testStrField=

(for the "sumbit-POST-request") csrf_token=blablablabla&testStrField=ggggggggggg&doCheckSign=Check+the+signed+data

So my question is - how actually validate_on_submit() determines whether it was "submit POST request" (have to return TRUE) or just-any-POST-request to website-backend (have to return FALSE but it doesn't)?

question from:https://stackoverflow.com/questions/65893771/validate-on-submit-from-flask-wtf-always-returns-true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to this example, you don't specify any requirement about your form. It should be like:

class SignForm(FlaskForm):
    testStrField = StringField(validators=[InputRequired()])
    doCheckSign = SubmitField(validators=[InputRequired()])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...