Welcome Guest, Not a member yet? Register   Sign In
Form Validation "dot array"
#3

(01-02-2022, 01:48 AM)kenjis Wrote: If you have 'addresses[X][country] ' where 'X' is the index,
it seems the validation rule 'addresses.country' should be 'addresses.*.country'.

https://codeigniter.com/user_guide/libra...are-arrays

I understand what this is about.
PHP Code:
$data = [
    'addresses' => [
        ['country' => ''],
        ['country' => 'USA'],
        ['country' => ''],
    ]
];

$v Services::validation();

$v->setRule('addresses.*.country''Address''required')->run($data);

// error 
// ['addresses.*.country' => "The Address field is required."] 

1. In fact, there are two errors, but one (the last) is displayed. Because the $field parameter is used for the error key.
2. It is impossible to determine which specific field the error applies to.

The error format should be like this
PHP Code:
$erros = [
    'addresses.0.country' => "The Address field is required.",
    'addresses.2.country' => "The Address field is required."
];
//or 
$erros = [
    'addresses' => [
            0 => ['country' => "The Address field is required."],
            2 => ['country' => "The Address field is required."],
    ]
]; 
I wanted to do a PR, but I think it will be tagged as BC.
Reply


Messages In This Thread
Form Validation "dot array" - by Kor - 12-28-2021, 03:14 PM
RE: Form Validation "dot array" - by kenjis - 01-02-2022, 01:48 AM
RE: Form Validation "dot array" - by iRedds - 01-02-2022, 02:49 PM
RE: Form Validation "dot array" - by kenjis - 01-02-2022, 05:42 PM
RE: Form Validation "dot array" - by iRedds - 01-02-2022, 06:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB