Skip to content

Is this expected behaviour on unsafe attributes validation? #20110

Open
@erdosam

Description

@erdosam

What steps will reproduce the problem?

Define an unsafe attribute with multiple rules in a model.

class SimpleModel extends Model
{
    public $account_id;
    private $count = 0;

    public function rules()
    {
        return [
            ['!account_id', 'required'],
            ['account_id', function () {
                Yii::info('The first message ' .$this->count++);
            }],
            ['account_id', function () {
                Yii::info('The second message ' .$this->count++);
            }]
        ];
    }
}

Prepare the unit test using Codeception.

    public function testSimpleModel()
    {
        $model = Yii::createObject([
            'class' => SimpleModel::class,
            'account_id' => 'TheAccount',
        ]);
        $model->load([], '');
        $model->validate();
    }

Run Codeception.

vendor/bin/codecept -c common run unit SimpleModelTest::testSimpleModel --debug

What's expected?

The validation should occur once for each rule on account_id.

  [application] 'The first message 0'
  [application] 'The second message 1'

What do you get instead?

  [application] 'The first message 0'
  [application] 'The first message 1'
  [application] 'The second message 2'
  [application] 'The second message 3'

Additional info

If all instances of account_id in the rules are marked with "!", the expected behavior is produced.

...
            ['!account_id', 'required'],
            ['!account_id', function () {
                Yii::info('The first message ' .$this->count++);
            }],
            ['!account_id', function () {
                Yii::info('The second message ' .$this->count++);
            }]
...
Q A
Yii version 2.0.15.1
PHP version 5.6.40
Operating system Debian GNU/Linux 9 (stretch)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions