Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The model attributes validation do not use custom validator. #20063

Open
mit40 opened this issue Nov 2, 2023 · 3 comments
Open

The model attributes validation do not use custom validator. #20063

mit40 opened this issue Nov 2, 2023 · 3 comments
Labels

Comments

@mit40
Copy link

mit40 commented Nov 2, 2023

What steps will reproduce the problem?

Class Test extends Model
...
 public function unique($attribute){...}
...
    public function rules()
    {
        return [
         ....
            ['propertyName', 'unique']
        ];
    }
...

see
file yiisoft/yii2/validators/Validator.php

 function createValidator
 line: 214     elseif (!isset(static::$builtInValidators[$type]) && $model->hasMethod($type))    // incorrect logic

What is the expected result?

use overwritten method

see version 2.0.9 :

 if ($type instanceof \Closure || $model->hasMethod($type)) {

What do you get instead?

it uses base method

Additional info

Q A
Yii version latest
PHP version PHP 8
Operating system linux
@mit40 mit40 changed the title The model attributes validation do not use custom Validator. The model attributes validation do not use custom validator. Nov 2, 2023
@xicond
Copy link
Contributor

xicond commented Nov 3, 2023

yeah, based on v2.0.9, you can override built in
but after v2.0.36, you cannot override builtin validator, because of #14370 issue
so you may change the custom name validator

@bizley
Copy link
Member

bizley commented Nov 3, 2023

Will changing the line 214 to

} elseif ($model->hasMethod($type)) {

fix this issue?

@samdark
Copy link
Member

samdark commented Nov 23, 2023

It was forbidden to override default validator while solving the issue: #14499 If it can be fixed without breaking that case again we can have it.

@samdark samdark added the type:bug Bug label Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants