Browse Source

New callback functions for form validation using Ajax.

close #4436
tags/2.0.0-rc
Thiago Talma 10 years ago committed by Carsten Brandt
parent
commit
88191bbe5c
  1. 1
      framework/CHANGELOG.md
  2. 16
      framework/assets/yii.activeForm.js
  3. 22
      framework/widgets/ActiveForm.php

1
framework/CHANGELOG.md

@ -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)

16
framework/assets/yii.activeForm.js

@ -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 @@
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 () {

22
framework/widgets/ActiveForm.php

@ -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
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);
} }

Loading…
Cancel
Save