16

I accustomed of using WTForms by means of Flask-WTF in my flask application. Doing server side validation is trivial. But how do I leverage this server validation to become a field level, ajax, client side validation? So, when user tab to another input fields, my application can directly goes on validating it and give validation warning/info/error.

I haven't found a resource in the internet yet

1
  • 1
    If your app needs to be as secure as possible, there is no such thing as redundancy between server and client side validation code. I usually do it for both. Commented Feb 22, 2017 at 22:13

2 Answers 2

16

A possible solution is as follows:

  • On the client side you attach a handler to the blur event in all the controls in the form.

  • Each time the blur event occurs you run a Javascript function that collects the values of all the fields and then submits them as an ajax POST request.

  • On the server the view function that handles this ajax POST request instantiates the Flask-WTF form and then validates it. Any errors that resulted from validation are collected into a dictionary that is then sent in a JSON response back to the client.

    For example, a successful validation could return the following JSON:

    { "errors": {} } 

    A response that includes errors could be:

    { "errors": { "name": "This field is required", "age": "Enter a numeric value between 0 and 99" } } 
  • The client gets this JSON response and applies the required changes to the DOM to expose the errors.

  • If you get a new blur event before the previous one returned you will probably want to abort the pending ajax POST and start a new one with updated field values. You should have only one pending validation request at a time to avoid race conditions.

Sign up to request clarification or add additional context in comments.

3 Comments

Great! Clear answer. Currently I am working on this also, and although the experience is a client side validation, actually it's a server side validation being called by ajax post request. I'll update my question once I finished this task. thanks.
Just to point out: While this approach keeps the validation code in one place (no need to write redundant code in JavaScript to do real client side validation), it does create a lot of traffic to the server, specially if forms grow large. On each blur a full round trip has to happen in which also the whole Flask/WTForms code has to be run. But as said, to have a real client side validation, one has to write more or less duplicate validation code in JavaScript. Choices...
@Miguel, Do you have any resources for such implementation? I'm trying to implement this solution in my validation process,
3

A great question. This is what we do (flask backend, jquery frontend):

  • use jquery.forms plugin to ajax forms. Pretty solid mature code. Shortcoming, cannot send json encoded data, only form-urlencoded. Receives plain or json data.
  • use wtfroms for form validation. Very mature codebase.
  • tried to use wtforms-json for accepting json, very screwy problems.

2 Comments

I've been considering wtforms-json. Could you expand on your experience with it?
Works for me. Pretty transparent, but I do specify concrete version of libraries. Since it patches other libraries, I'd be vary to let the package be updated to the latest...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.