Commit f7b8595f by resurtm

Validator::clientValidateAttribute() now accepts third parameter: a view.

punycode.js asset bundle registering moved from ActiveField to validators.
parent 6458b8df
......@@ -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,
......
......@@ -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);
......
......@@ -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);
......
......@@ -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),
......
......@@ -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;
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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);
......
......@@ -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) {
......
......@@ -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;
......
......@@ -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 {
......
......@@ -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;
}
......
......@@ -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 <qiang.xue@gmail.com>
......@@ -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;
}
......
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