Make validator getClientOptions public

This allows implementing custom client-side validation
without extending every validator.

Fixes #11163
close #13145
This commit is contained in:
Nikola Kovacs
2016-12-06 11:21:20 +01:00
committed by Carsten Brandt
parent 0e5efb91eb
commit f5beaf3edf
18 changed files with 24 additions and 21 deletions

View File

@ -895,13 +895,13 @@ the former will take precedence.
> - if you want to implement your own custom client-side validation but leave the synchronization with server-side > - if you want to implement your own custom client-side validation but leave the synchronization with server-side
> validator options; > validator options;
> - to extend or customize to fit your specific needs: > - to extend or customize to fit your specific needs:
> >
> ```php > ```php
> protected function getClientOptions($model, $attribute) > public function getClientOptions($model, $attribute)
> { > {
> $options = parent::getClientOptions($model, $attribute); > $options = parent::getClientOptions($model, $attribute);
> // Modify $options here > // Modify $options here
> >
> return $options; > return $options;
> } > }
> ``` > ```

View File

@ -37,7 +37,7 @@ Yii Framework 2 Change Log
- Enh #9162: Added support of closures in `value` for attributes in `yii\widgets\DetailView` (arogachev) - Enh #9162: Added support of closures in `value` for attributes in `yii\widgets\DetailView` (arogachev)
- Enh #10896: Select only primary key when counting records in UniqueValidator (developeruz) - Enh #10896: Select only primary key when counting records in UniqueValidator (developeruz)
- Enh #11037: `yii.js` and `yii.validation.js` use `Regexp.test()` instead of `String.match()` (arogachev, nkovacs) - Enh #11037: `yii.js` and `yii.validation.js` use `Regexp.test()` instead of `String.match()` (arogachev, nkovacs)
- Enh #11163: Added separate method for client-side validation options in `yii\validators\Validator` (arogachev) - Enh #11163: Added separate method for client-side validation options `yii\validators\Validator::getClientOptions()` (arogachev)
- Enh #11756: Added type mapping for `varbinary` data type in MySQL DBMS (silverfire) - Enh #11756: Added type mapping for `varbinary` data type in MySQL DBMS (silverfire)
- Enh #11929: Changed `type` column type from `int` to `smallInt` in RBAC migrations (silverfire) - Enh #11929: Changed `type` column type from `int` to `smallInt` in RBAC migrations (silverfire)
- Enh #12015: Changed visibility `yii\db\ActiveQueryTrait::createModels()` from private to protected (ArekX, dynasource) - Enh #12015: Changed visibility `yii\db\ActiveQueryTrait::createModels()` from private to protected (ArekX, dynasource)

View File

@ -29,14 +29,14 @@ The simple way to upgrade Yii, for example to version 2.0.10 (replace this with
composer require "yiisoft/yii2:~2.0.10" composer require "yiisoft/yii2:~2.0.10"
This however may fail due to changes in the dependencies of yiisoft/yii2, which may change due to security updates This however may fail due to changes in the dependencies of yiisoft/yii2, which may change due to security updates
in other libraries or by adding support for newer versions. `composer require` will not update any other packages in other libraries or by adding support for newer versions. `composer require` will not update any other packages
as a safety feature. as a safety feature.
The better way to upgrade is to change the `composer.json` file to require the new Yii version and then The better way to upgrade is to change the `composer.json` file to require the new Yii version and then
run `composer update` by specifying all packages that are allowed to be updated. run `composer update` by specifying all packages that are allowed to be updated.
composer update yiisoft/yii2 yiisoft/yii2-composer bower-asset/jquery.inputmask composer update yiisoft/yii2 yiisoft/yii2-composer bower-asset/jquery.inputmask
The above command will only update the specified packages and leave the versions of all other dependencies intact. The above command will only update the specified packages and leave the versions of all other dependencies intact.
This helps to update packages step by step without causing a lot of package version changes that might break in some way. This helps to update packages step by step without causing a lot of package version changes that might break in some way.
If you feel lucky you can of course update everything to the latest version by running `composer update` without If you feel lucky you can of course update everything to the latest version by running `composer update` without
@ -57,6 +57,9 @@ Upgrade from Yii 2.0.10
This method is implemented in the `yii\db\QueryTrait`, so this only affects your code if you implement QueryInterface This method is implemented in the `yii\db\QueryTrait`, so this only affects your code if you implement QueryInterface
in a class that does not use the trait. in a class that does not use the trait.
* `yii\validators\FileValidator::getClientOptions()` and `yii\validators\ImageValidator::getClientOptions()` are now public.
If you extend from these classes and override these methods, you must make them public as well.
Upgrade from Yii 2.0.9 Upgrade from Yii 2.0.9
---------------------- ----------------------

View File

@ -95,7 +95,7 @@ class CaptchaValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$captcha = $this->createCaptchaAction(); $captcha = $this->createCaptchaAction();
$code = $captcha->getVerifyCode(false); $code = $captcha->getVerifyCode(false);

View File

@ -79,7 +79,7 @@ class BooleanValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$options = [ $options = [
'trueValue' => $this->trueValue, 'trueValue' => $this->trueValue,

View File

@ -234,7 +234,7 @@ class CompareValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$options = [ $options = [
'operator' => $this->operator, 'operator' => $this->operator,

View File

@ -121,7 +121,7 @@ class EmailValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$options = [ $options = [
'pattern' => new JsExpression($this->pattern), 'pattern' => new JsExpression($this->pattern),

View File

@ -386,7 +386,7 @@ class FileValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$label = $model->getAttributeLabel($attribute); $label = $model->getAttributeLabel($attribute);

View File

@ -97,7 +97,7 @@ class FilterValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$options = []; $options = [];
if ($this->skipOnEmpty) { if ($this->skipOnEmpty) {

View File

@ -172,7 +172,7 @@ class ImageValidator extends FileValidator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$options = parent::getClientOptions($model, $attribute); $options = parent::getClientOptions($model, $attribute);

View File

@ -597,7 +597,7 @@ class IpValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$messages = [ $messages = [
'ipv6NotAllowed' => $this->ipv6NotAllowed, 'ipv6NotAllowed' => $this->ipv6NotAllowed,

View File

@ -130,7 +130,7 @@ class NumberValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$label = $model->getAttributeLabel($attribute); $label = $model->getAttributeLabel($attribute);

View File

@ -117,7 +117,7 @@ class RangeValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$range = []; $range = [];
foreach ($this->range as $value) { foreach ($this->range as $value) {

View File

@ -74,7 +74,7 @@ class RegularExpressionValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$pattern = Html::escapeJsRegularExpression($this->pattern); $pattern = Html::escapeJsRegularExpression($this->pattern);

View File

@ -97,7 +97,7 @@ class RequiredValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$options = []; $options = [];
if ($this->requiredValue !== null) { if ($this->requiredValue !== null) {

View File

@ -159,7 +159,7 @@ class StringValidator extends Validator
return 'yii.validation.string(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; return 'yii.validation.string(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
} }
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
$label = $model->getAttributeLabel($attribute); $label = $model->getAttributeLabel($attribute);

View File

@ -125,7 +125,7 @@ class UrlValidator extends Validator
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
if (strpos($this->pattern, '{schemes}') !== false) { if (strpos($this->pattern, '{schemes}') !== false) {
$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);

View File

@ -372,7 +372,7 @@ class Validator extends Component
* @return array the client-side validation options * @return array the client-side validation options
* @since 2.0.11 * @since 2.0.11
*/ */
protected function getClientOptions($model, $attribute) public function getClientOptions($model, $attribute)
{ {
return []; return [];
} }