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

@ -897,7 +897,7 @@ the former will take precedence.
> - to extend or customize to fit your specific needs:
>
> ```php
> protected function getClientOptions($model, $attribute)
> public function getClientOptions($model, $attribute)
> {
> $options = parent::getClientOptions($model, $attribute);
> // Modify $options here

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 #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 #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 #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)

View File

@ -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
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
----------------------

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -97,7 +97,7 @@ class RequiredValidator extends Validator
/**
* @inheritdoc
*/
protected function getClientOptions($model, $attribute)
public function getClientOptions($model, $attribute)
{
$options = [];
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) . ');';
}
protected function getClientOptions($model, $attribute)
public function getClientOptions($model, $attribute)
{
$label = $model->getAttributeLabel($attribute);

View File

@ -125,7 +125,7 @@ class UrlValidator extends Validator
/**
* @inheritdoc
*/
protected function getClientOptions($model, $attribute)
public function getClientOptions($model, $attribute)
{
if (strpos($this->pattern, '{schemes}') !== false) {
$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
* @since 2.0.11
*/
protected function getClientOptions($model, $attribute)
public function getClientOptions($model, $attribute)
{
return [];
}