Recently i had to deal with a problem of dynamically marking form fields with error and displaying specific error for this field below, in case of validation error. After trying to find alredy existing solution and 2 hours spend on stackoverflow.com looking for answer with no success i started to build my own solution.
In my example, I will create a blog post dynamically by using ajax to fill a form with the needed data; submitting it must return status code :ok(200) and create a post. In case you have any validation errors, the corresponding inputs must be marked with an error and the text of the error must be put below the input field. Take a look at this picture to better understand what I mean:
Now, if you have any validation errors, for example “field can’t be blank” or a length limitation for the specific field, you will see it marked with an error, like in the picture above. Also, you can use the data attribute – error_on to enable or disable this field to be marked with an error. If you have some custom validators in the post model that doesn’t corresponds with any of the fields in the form, it will be automatically added to general errors. #form-errors