From f7b8595f768488f04885fc6744c34a36d360dcdc Mon Sep 17 00:00:00 2001 From: resurtm Date: Wed, 15 May 2013 23:17:50 +0600 Subject: [PATCH] Validator::clientValidateAttribute() now accepts third parameter: a view. punycode.js asset bundle registering moved from ActiveField to validators. --- yii/validators/BooleanValidator.php | 4 +++- yii/validators/CaptchaValidator.php | 4 +++- yii/validators/CompareValidator.php | 4 +++- yii/validators/EmailValidator.php | 8 +++++++- yii/validators/InlineValidator.php | 4 +++- yii/validators/NumberValidator.php | 4 +++- yii/validators/RangeValidator.php | 4 +++- yii/validators/RegularExpressionValidator.php | 4 +++- yii/validators/RequiredValidator.php | 4 +++- yii/validators/StringValidator.php | 4 +++- yii/validators/UrlValidator.php | 8 +++++++- yii/validators/Validator.php | 4 +++- yii/widgets/ActiveField.php | 11 +---------- 13 files changed, 45 insertions(+), 22 deletions(-) diff --git a/yii/validators/BooleanValidator.php b/yii/validators/BooleanValidator.php index 2929dfd..3fc8c72 100644 --- a/yii/validators/BooleanValidator.php +++ b/yii/validators/BooleanValidator.php @@ -79,9 +79,11 @@ class BooleanValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $options = array( 'trueValue' => $this->trueValue, diff --git a/yii/validators/CaptchaValidator.php b/yii/validators/CaptchaValidator.php index c49ffdb..f446d5f 100644 --- a/yii/validators/CaptchaValidator.php +++ b/yii/validators/CaptchaValidator.php @@ -91,9 +91,11 @@ class CaptchaValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $captcha = $this->getCaptchaAction(); $code = $captcha->getVerifyCode(false); diff --git a/yii/validators/CompareValidator.php b/yii/validators/CompareValidator.php index 7f7318f..2fce4fc 100644 --- a/yii/validators/CompareValidator.php +++ b/yii/validators/CompareValidator.php @@ -178,9 +178,11 @@ class CompareValidator extends Validator * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated * @return string the client-side validation script + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @throws InvalidConfigException if CompareValidator::operator is invalid */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $options = array('operator' => $this->operator); diff --git a/yii/validators/EmailValidator.php b/yii/validators/EmailValidator.php index f1059d7..141b747 100644 --- a/yii/validators/EmailValidator.php +++ b/yii/validators/EmailValidator.php @@ -113,10 +113,16 @@ class EmailValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { + if ($this->enableIDN) { + $view->registerAssetBundle('punycode'); + } + $options = array( 'pattern' => new JsExpression($this->pattern), 'fullPattern' => new JsExpression($this->fullPattern), diff --git a/yii/validators/InlineValidator.php b/yii/validators/InlineValidator.php index 8af5bbc..dd951aa 100644 --- a/yii/validators/InlineValidator.php +++ b/yii/validators/InlineValidator.php @@ -79,12 +79,14 @@ class InlineValidator extends Validator * * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. Null if the validator does not support * client-side validation. * @see enableClientValidation * @see \yii\web\ActiveForm::enableClientValidation */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { if ($this->clientValidate !== null) { $method = $this->clientValidate; diff --git a/yii/validators/NumberValidator.php b/yii/validators/NumberValidator.php index 33822bf..24735ec 100644 --- a/yii/validators/NumberValidator.php +++ b/yii/validators/NumberValidator.php @@ -114,9 +114,11 @@ class NumberValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $label = $object->getAttributeLabel($attribute); $value = $object->$attribute; diff --git a/yii/validators/RangeValidator.php b/yii/validators/RangeValidator.php index 4bed303..9cf4cb3 100644 --- a/yii/validators/RangeValidator.php +++ b/yii/validators/RangeValidator.php @@ -81,9 +81,11 @@ class RangeValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $range = array(); foreach ($this->range as $value) { diff --git a/yii/validators/RegularExpressionValidator.php b/yii/validators/RegularExpressionValidator.php index 505812f..d28dc22 100644 --- a/yii/validators/RegularExpressionValidator.php +++ b/yii/validators/RegularExpressionValidator.php @@ -79,10 +79,12 @@ class RegularExpressionValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. * @throws InvalidConfigException if the "pattern" is not a valid regular expression */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $pattern = $this->pattern; $pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern); diff --git a/yii/validators/RequiredValidator.php b/yii/validators/RequiredValidator.php index 424f94c..797da2e 100644 --- a/yii/validators/RequiredValidator.php +++ b/yii/validators/RequiredValidator.php @@ -102,9 +102,11 @@ class RequiredValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $options = array(); if ($this->requiredValue !== null) { diff --git a/yii/validators/StringValidator.php b/yii/validators/StringValidator.php index 110619e..9982fc1 100644 --- a/yii/validators/StringValidator.php +++ b/yii/validators/StringValidator.php @@ -126,9 +126,11 @@ class StringValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { $label = $object->getAttributeLabel($attribute); $value = $object->$attribute; diff --git a/yii/validators/UrlValidator.php b/yii/validators/UrlValidator.php index ddc3e86..d80238e 100644 --- a/yii/validators/UrlValidator.php +++ b/yii/validators/UrlValidator.php @@ -110,11 +110,17 @@ class UrlValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. * @see \yii\Web\ActiveForm::enableClientValidation */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { + if ($this->enableIDN) { + $view->registerAssetBundle('punycode'); + } + if (strpos($this->pattern, '{schemes}') !== false) { $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); } else { diff --git a/yii/validators/Validator.php b/yii/validators/Validator.php index 677191b..6b103bf 100644 --- a/yii/validators/Validator.php +++ b/yii/validators/Validator.php @@ -211,11 +211,13 @@ abstract class Validator extends Component * * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. + * @param \yii\base\View $view the view object that is going to be used to render views or view files + * containing a model form with this validator applied. * @return string the client-side validation script. Null if the validator does not support * client-side validation. * @see \yii\web\ActiveForm::enableClientValidation */ - public function clientValidateAttribute($object, $attribute) + public function clientValidateAttribute($object, $attribute, $view) { return null; } diff --git a/yii/widgets/ActiveField.php b/yii/widgets/ActiveField.php index d2254fe..45faf9d 100644 --- a/yii/widgets/ActiveField.php +++ b/yii/widgets/ActiveField.php @@ -11,8 +11,6 @@ use yii\db\ActiveRecord; use yii\helpers\Html; use yii\base\Model; use yii\web\JsExpression; -use yii\validators\EmailValidator; -use yii\validators\UrlValidator; /** * @author Qiang Xue @@ -123,13 +121,6 @@ class ActiveField extends Component } $options['class'] = implode(' ', $class); - foreach ($this->model->getActiveValidators($attribute) as $validator) { - if (($validator instanceof EmailValidator || $validator instanceof UrlValidator) && $validator->enableIDN) { - $this->form->view->registerAssetBundle('punycode'); - break; - } - } - return Html::beginTag($this->tag, $options); } @@ -147,7 +138,7 @@ class ActiveField extends Component $validators = array(); foreach ($this->model->getActiveValidators($attribute) as $validator) { /** @var \yii\validators\Validator $validator */ - $js = $validator->clientValidateAttribute($this->model, $attribute); + $js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView()); if ($validator->enableClientValidation && $js != '') { $validators[] = $js; }