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

Bug: validation level dont work as expected #9022

Closed
mozzar opened this issue Jul 4, 2024 · 6 comments
Closed

Bug: validation level dont work as expected #9022

mozzar opened this issue Jul 4, 2024 · 6 comments

Comments

@mozzar
Copy link

mozzar commented Jul 4, 2024

PHP Version

8.3

CodeIgniter4 Version

5.0.2

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

MariaDB 10.6.18

What happened?

Validation does not work at higher levels of the array
look at the screenshot of my rules:
obraz

here is my request:
obraz

and here is validation:
obraz

Steps to Reproduce

create rules, eg:

$rules = [
            'cards' => 'required',
            'cards.*.fields' => 'required',
            'cards.*.fields.*' => 'required',
        ];

create json body for request:

$body = '{
  "cards": [
    {
      "fields": {"name": "name"}
    }
]
}';

write validate code:

$validation = service('validation');
$validation->setRules($rules);
 if (!$validation->run(json_decode($body, true))) {
            // handle validation errors
            print_r($validation->getErrors());
}

Expected Output

Validation should not show an error. Unless there are no "fields"

Anything else?

thanks.

@mozzar mozzar added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 4, 2024
@kenjis
Copy link
Member

kenjis commented Jul 4, 2024

Thank you for reporting.

Please use the following format in the future.

```php
// PHP code
```

@kenjis
Copy link
Member

kenjis commented Jul 4, 2024

In your sample data, there is no cards.*.fields field.

        $body = '{"cards": [{"fields": {"name": "name"}}]}';
        $data = json_decode($body, true);
        var_dump(array_flatten_with_dots($data));
array (size=1)
  'cards.0.fields.name' => string 'name' (length=4)

@kenjis
Copy link
Member

kenjis commented Jul 4, 2024

@mozzar Can you describe the format of the data that you want to validate?

Also, there is a known bug #8006 in Validation with *.

@mozzar
Copy link
Author

mozzar commented Jul 5, 2024

@mozzar Can you describe the format of the data that you want to validate?

Also, there is a known bug #8006 in Validation with *.

In your sample data, there is no cards.*.fields field.

        $body = '{"cards": [{"fields": {"name": "name"}}]}';
        $data = json_decode($body, true);
        var_dump(array_flatten_with_dots($data));
array (size=1)
  'cards.0.fields.name' => string 'name' (length=4)

so I can't validate that key "fields" exists?

here is my data dumped with var_dump:

array(1) {
  ["cards"]=>
  array(1) {
    [0]=>
    array(1) {
      ["fields"]=>
      array(1) {
        ["name"]=>
        string(5) "value"
      }
    }
  }
}

it seems to me that it should be possible to add a rule that checks whether the key "fields" exists or not.
"field_exists" mentioned by you in #8006 solves my problem

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 5, 2024
@kenjis
Copy link
Member

kenjis commented Jul 5, 2024

The data is:

'cards.0.fields.name' => 'value'

If all data is like that, you can write the validation rule:

[
    'cards.0.fields.name' => 'required',
]

so I can't validate that key "fields" exists?

You can use the rule field_exists.

@mozzar
Copy link
Author

mozzar commented Jul 5, 2024

The data is:

'cards.0.fields.name' => 'value'

If all data is like that, you can write the validation rule:

[
    'cards.0.fields.name' => 'required',
]

so I can't validate that key "fields" exists?

You can use the rule field_exists.

I edited my previous comment, yes it fixes my problem.
sorry for the unnecessary post.
I will close this issue.

@mozzar mozzar closed this as completed Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants