Skip to content

Bug: [Validation] field with asterisk (nested array) throws exception #5922

Description

@mostafakhudair

PHP Version

7.4

CodeIgniter4 Version

4.1.9

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

MySQL 4.9

What happened?

    protected array $validationRules = [
        'customer_account_number'                 => [
            'label' => 'ACCOUNT NUMBER',
            'rules' => 'required|exact_length[5]',
        ],
        'debit_amount'                            => [
            'label' => 'DEBIT AMOUNT',
            'rules' => 'required|decimal|is_natural_no_zero',
        ],
        'beneficiaries_accounts.*.account_number' => [
            'label' => 'BENEFICIARY ACCOUNT NUMBER',
            'rules' => 'exact_length[5]',
        ],
        'beneficiaries_accounts.*.credit_amount'  => [
            'label' => 'CREDIT AMOUNT',
            'rules' => 'required|decimal|is_natural_no_zero',
        ],
        'beneficiaries_accounts.*.purpose'        => [
            'label' => 'PURPOSE',
            'rules' => 'required_without[beneficiaries_accounts.*.account_number]|min_length[3]|max_length[255]',
        ],
    ];
    $this->validation->setRules($model->getValidationRules());

Case 1:

    $data = [
        "customer_account_number" => "A_490",
        "debit_amount"            => "1500",
        "beneficiaries_accounts"  => [],
    ];

Case 2:

    $data = [
        "customer_account_number" => "A_490",
        "debit_amount"            => "1500",
        "beneficiaries_accounts"  => [
            "account_1" => [
                "account_number" => "A_103",
                "credit_amount"  => 1000,
                "purpose"        => "",
            ],
        ],
    ];

Case 3:

    $data = [
        "customer_account_number" => "A_490",
        "debit_amount"            => "1500",
        "beneficiaries_accounts"  => [
            "account_1" => [
                "account_number" => "A_103",
                "credit_amount"  => 1000,
                "purpose"        => "Personal",
            ],
            "account_2" => [
                "account_number" => "A_258",
                "credit_amount"  => null,
                "purpose"        => "A",
            ],
        ],
    ];
    if (! $this->validation->run($data))) {
        if ($this->validation->hasError())) {
            return $this->validation->listErrors();
        }

        return "Success";
    }

Steps to Reproduce

Case 1: validation throws an exception: Argument 1 passed to CodeIgniter\Validation\Rules::exact_length() must be of the type string or null, array given, called in .*/vendor/codeigniter4/framework/system/Validation/Validation.php on line 292.

Case 2:
beneficiaries_accounts.account_1.account_number validation successfully as expected
beneficiaries_accounts.account_1.credit_amount validation successfully as expected.
beneficiaries_accounts.account_1.purpose validation successfully as expected.

Case 3:
beneficiaries_accounts.account_1.account_number validation successfully as expected
beneficiaries_accounts.account_1.credit_amount validation successfully as expected.
beneficiaries_accounts.account_1.purpose validation successfully as expected.

beneficiaries_accounts.account_2.account_number validation successfully as expected.
beneficiaries_accounts.account_2.credit_amount validation successfully while value is null, and it's ignored required rule.
beneficiaries_accounts.account_2.purpose validation fails Purpose field must be at least 3 characters in length, and it's ignored required_without rule.

Expected Output

Case 1: expect validation fails without throws an exception

Case 3: validate each array separately

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions