mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 05:48:11 +08:00
Make validator getClientOptions public
This allows implementing custom client-side validation without extending every validator. Fixes #11163 close #13145
This commit is contained in:
committed by
Carsten Brandt
parent
0e5efb91eb
commit
f5beaf3edf
@ -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;
|
||||||
> }
|
> }
|
||||||
> ```
|
> ```
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
@ -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
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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,
|
||||||
|
|||||||
@ -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,
|
||||||
|
|||||||
@ -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),
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
|
|||||||
@ -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,
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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 [];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user