Commit 88191bbe by Thiago Talma Committed by Carsten Brandt

New callback functions for form validation using Ajax.

close #4436
parent 42eba969
...@@ -152,6 +152,7 @@ Yii Framework 2 Change Log ...@@ -152,6 +152,7 @@ Yii Framework 2 Change Log
- Enh #4297: Added check for DOM extension to requirements (samdark) - Enh #4297: Added check for DOM extension to requirements (samdark)
- Enh #4317: Added `absoluteAuthTimeout` to yii\web\User (ivokund, nkovacs) - Enh #4317: Added `absoluteAuthTimeout` to yii\web\User (ivokund, nkovacs)
- Enh #4360: Added client validation support for file validator (Skysplit) - Enh #4360: Added client validation support for file validator (Skysplit)
- Enh #4436: Added callback functions to AJAX-based form validation (thiagotalma)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......
...@@ -44,6 +44,12 @@ ...@@ -44,6 +44,12 @@
// a callback that is called after an attribute is validated. The signature of the callback should be: // a callback that is called after an attribute is validated. The signature of the callback should be:
// function ($form, attribute, messages) // function ($form, attribute, messages)
afterValidate: undefined, afterValidate: undefined,
// a pre-request callback function on AJAX-based validation. The signature of the callback should be:
// function ($form, jqXHR, textStatus)
ajaxBeforeSend: undefined,
// a function to be called when the request finishes on AJAX-based validation. The signature of the callback should be:
// function ($form, jqXHR, textStatus)
ajaxComplete: undefined,
// the GET parameter name indicating an AJAX-based validation // the GET parameter name indicating an AJAX-based validation
ajaxParam: 'ajax', ajaxParam: 'ajax',
// the type of data that you're expecting back from the server // the type of data that you're expecting back from the server
...@@ -306,6 +312,16 @@ ...@@ -306,6 +312,16 @@
type: $form.prop('method'), type: $form.prop('method'),
data: $form.serialize() + extData, data: $form.serialize() + extData,
dataType: data.settings.ajaxDataType, dataType: data.settings.ajaxDataType,
complete: function (jqXHR, textStatus) {
if (data.settings.ajaxComplete) {
data.settings.ajaxComplete($form, jqXHR, textStatus);
}
},
beforeSend: function (jqXHR, textStatus) {
if (data.settings.ajaxBeforeSend) {
data.settings.ajaxBeforeSend($form, jqXHR, textStatus);
}
},
success: function (msgs) { success: function (msgs) {
if (msgs !== null && typeof msgs === 'object') { if (msgs !== null && typeof msgs === 'object') {
$.each(data.attributes, function () { $.each(data.attributes, function () {
......
...@@ -152,6 +152,26 @@ class ActiveForm extends Widget ...@@ -152,6 +152,26 @@ class ActiveForm extends Widget
*/ */
public $afterValidate; public $afterValidate;
/** /**
* @var string|JsExpression a JS pre-request callback function on AJAX-based validation.
* The signature of the callback should be:
*
* ~~~
* function ($form, jqXHR, textStatus) {
* }
* ~~~
*/
public $ajaxBeforeSend;
/**
* @var string|JsExpression a JS callback to be called when the request finishes on AJAX-based validation.
* The signature of the callback should be:
*
* ~~~
* function ($form, jqXHR, textStatus) {
* }
* ~~~
*/
public $ajaxComplete;
/**
* @var array the client validation options for individual attributes. Each element of the array * @var array the client validation options for individual attributes. Each element of the array
* represents the validation options for a particular attribute. * represents the validation options for a particular attribute.
* @internal * @internal
...@@ -208,7 +228,7 @@ class ActiveForm extends Widget ...@@ -208,7 +228,7 @@ class ActiveForm extends Widget
if ($this->validationUrl !== null) { if ($this->validationUrl !== null) {
$options['validationUrl'] = Url::to($this->validationUrl); $options['validationUrl'] = Url::to($this->validationUrl);
} }
foreach (['beforeSubmit', 'beforeValidate', 'afterValidate'] as $name) { foreach (['beforeSubmit', 'beforeValidate', 'afterValidate', 'ajaxBeforeSend', 'ajaxComplete'] as $name) {
if (($value = $this->$name) !== null) { if (($value = $this->$name) !== null) {
$options[$name] = $value instanceof JsExpression ? $value : new JsExpression($value); $options[$name] = $value instanceof JsExpression ? $value : new JsExpression($value);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment