python - Validate WTForm before submitting -


is possible validate wtform field after leaving field before submit? example, after entering username, field validated see if available , shows checkmark, before user clicks submit.

when field changed, perform check , change text in adjacent node. things can validated directly in browser. validate against data on server, send request javascript view checks data , returns json response.

@app.route('/username-exists', methods=['post']) def username_exists():     username = request.form['username']     exists = check_if_user_exists(username)     return jsonify(exists=exists) 
<input id='username' name='username'> <p id='username-status'></p> 
var username_input = $('#username'); var username_status = $('#username-status');  $('#username').on('focusout', function () {     $.post(         "{{ url_for('username_exists') }}",         {             username: username_input.val()         },         function (data) {             username_status.text(data.exists ? '✔️' : '🙅');         }     ); }); 

this example uses jquery, concept not specific library.

alternatively, post entire form separate view validates fields, return jsonify(form.errors) , them in browser. code same above, logic put error messages next correct fields.

remember still validate data when form submitted, requests can made outside browser other


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -