From c0746f0689ebb9734bb14bed3c5cf14526824f50 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 29 Jun 2013 17:48:11 -0400 Subject: [PATCH] Fixes issue #588: Added afterValidate to ActiveForm. --- framework/yii/assets/yii.activeForm.js | 6 ++++++ framework/yii/widgets/ActiveForm.php | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/framework/yii/assets/yii.activeForm.js b/framework/yii/assets/yii.activeForm.js index 1a2e58d..2b08d53 100644 --- a/framework/yii/assets/yii.activeForm.js +++ b/framework/yii/assets/yii.activeForm.js @@ -41,6 +41,9 @@ // a callback that is called before validating each attribute. The signature of the callback should be: // function ($form, attribute, messages) { ...return false to cancel the validation...} beforeValidate: undefined, + // a callback that is called after an attribute is validated. The signature of the callback should be: + // function ($form, attribute, messages) + afterValidate: undefined, // the GET parameter name indicating an AJAX-based validation ajaxVar: 'ajax' }; @@ -333,6 +336,9 @@ $input = findInput($form, attribute), hasError = false; + if (data.settings.afterValidate) { + data.settings.afterValidate($form, attribute, messages); + } attribute.status = 1; if ($input.length) { hasError = messages && $.isArray(messages[attribute.name]) && messages[attribute.name].length; diff --git a/framework/yii/widgets/ActiveForm.php b/framework/yii/widgets/ActiveForm.php index eb14293..d844117 100644 --- a/framework/yii/widgets/ActiveForm.php +++ b/framework/yii/widgets/ActiveForm.php @@ -12,6 +12,7 @@ use yii\base\Widget; use yii\base\Model; use yii\helpers\Html; use yii\helpers\Json; +use yii\web\JsExpression; /** * ActiveForm ... @@ -103,6 +104,38 @@ class ActiveForm extends Widget */ public $ajaxVar = 'ajax'; /** + * @var string|JsExpression a JS callback that will be called when the form is being submitted. + * The signature of the callback should be: + * + * ~~~ + * function ($form) { + * ...return false to cancel submission... + * } + * ~~~ + */ + public $beforeSubmit; + /** + * @var string|JsExpression a JS callback that is called before validating an attribute. + * The signature of the callback should be: + * + * ~~~ + * function ($form, attribute, messages) { + * ...return false to cancel the validation... + * } + * ~~~ + */ + public $beforeValidate; + /** + * @var string|JsExpression a JS callback that is called after validating an attribute. + * The signature of the callback should be: + * + * ~~~ + * function ($form, attribute, messages) { + * } + * ~~~ + */ + public $afterValidate; + /** * @var array the client validation options for individual attributes. Each element of the array * represents the validation options for a particular attribute. * @internal @@ -157,6 +190,11 @@ class ActiveForm extends Widget if ($this->validationUrl !== null) { $options['validationUrl'] = Html::url($this->validationUrl); } + foreach (array('beforeSubmit', 'beforeValidate', 'afterValidate') as $name) { + if (($value = $this->$name) !== null) { + $options[$name] = $value instanceof JsExpression ? $value : new JsExpression($value); + } + } return $options; }