codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: [Model] setValidationRule() cannot use with ruleGroup #7689

Closed serge1978 closed 1 year ago

serge1978 commented 1 year ago

PHP Version

8.2

CodeIgniter4 Version

4.3.6

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

MySQL 8.0.28

What happened?

Model::setValidationRule() method doesn't work and leads to error for me:

Link for method in user guide: https://codeigniter.com/user_guide/models/model.html?highlight=setvalidationrule#CodeIgniter\Model::setValidationRule

Steps to Reproduce

I'm trying to use Model::setValidationRule() method inside of model. For example:

class ExampleModel extends Model
{

    protected $validationRules = 'rulesGroup'; // group is in .../app/Config/Validation.php

    public function doSomethingWithData(array $data): bool
    {
        $fieldRules = [
            'label' => 'FieldLabel',
            'rules' => ['required', 'max_length[5]'],
        ];

        $this->setValidationRule('fieldName', $fieldRules);

        // .... do some job

        return true; // or false if something is wrong
    }

}

Expected Output

As I understood from the user guide I expected to override validation rules for the specified field dynamically, declared earlier in $validationRules property...

Anything else?

No response

kenjis commented 1 year ago

What is the exact error message?

kenjis commented 1 year ago

@serge1978 Please try: #7691

kenjis commented 1 year ago

As a workaround:

serge1978 commented 1 year ago

What is the exact error message?

 "message": "Cannot access offset of type string on string",
"file": ".../vendor/codeigniter4/framework/system/BaseModel.php",
serge1978 commented 1 year ago

@kenjis , thanks!

serge1978 commented 1 year ago

@kenjis, oh, forgot to ask... Is this behavior currently a bug or a feature? Will setValidationRule() method work with ruleGroup in future release?