Welcome Guest, Not a member yet? Register   Sign In
Strange logic of CI Model with useCasts and validate for json-array field
#1

Hi everybody. I faced the following problem in CI 4.5.5. I use validation and casts on the CI Model side here is an example code:

PHP Code:
protected array $casts = [
        'id'            => 'int',
        'profile'      => '?json-array',
        'extra'        => '?json-array',
        'expires'      => 'int',
        'created_by_id' => 'int',
        'updated_by_id' => 'int',
        'roleId'        => 'int',
        'moduleId'      => 'int',
        'selfAuth'      => 'int',
    ];

protected 
$validationRules = [
        'profile.surname' => [
            'label' => 'surname',
            'rules' => 'required|max_length[255]',
        ],
        'profile.name' => [
            'label' => 'name',
            'rules' => 'required|max_length[255]',
        ],
        'profile.patronymic' => [
            'label' => 'patronymic',
            'rules' => 'permit_empty|max_length[255]',
        ],
        'phone' => [
            'label'  => 'phone',
            'rules'  => 'required|mob_phone|is_unique[users.phone]',
        ],
        'email' => [
            'label'  => 'Email',
            'rules'  => 'required|max_length[255]|valid_email|is_unique[users.email]',
        ],
        'password' => [
            'label' => 'Пароль',
            'rules' => 'required|verify_password',
        ],
        'password_conf' => [
            'label'  => 'password conf',
            'rules'  => 'required|matches[password]',
        ],
    ]; 

When I try to insert data, a validation error occurs: profile.surname and profile.name - are required fields to fill in. After examining the BaseModel source code, I see that the transformDataToArray method casts profile values into a string and after that data validation occurs and, accordingly, an error occurs. As for me, the logic of the sequence of processes is strange. I understand that the validation method expects an array, but why casts it before that and is it a Bug or did I not understand the logic?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB