AJAX validation
Some validations can only be done on the server-side, because only the server has the necessary information. For example, to validate that a company name or user e-mail is unique, we have to check the corresponding tables on the server side. In this case, you should use built-in AJAX validation. Yii2 supports AJAX form validation, which essentially sends the form values to the server, validates them, and sends back the validation errors, all without leaving the page. It does this every time you tab out of a (changed) field.
Getting ready
Create a new application by using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-startinstallation.html.
How to do it...
In the basic app template, we have a simple contact form. You can see this page at
http://yii-book.app/index.php?r=site/contact
. Open and modify the related view form,@app/views/site/contact.php
. To enable AJAX validation for the whole form, set up theenableAjaxValidation...