Welcome Guest, Not a member yet? Register   Sign In
Function inside a validation rule
#1

(This post was last modified: 01-03-2023, 04:02 PM by Mobostar.)

Hi,
This type of custom validation rule used to work in CI3 but I am getting an error with CI4.
Code:
PHP Code:
$validation->setRrules([
    'room' => [
        'label'    => 'Room',
        'rules'    => [
            'required',
            [
                'validateRoom',
                function ($str) use (&$room_list) {
                    return (in_array($str$room_list));
 
                }
            ]
        ]
    ]
]); 

I am getting the following

Quote:preg_match(): Argument #2 ($subject) must be of type string, array given

Quote:SYSTEMPATH/Validation/Validation.php at line 292
Code:
292             if (! $isCallable && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {
293                 $rule  = $match[1];
294                 $param = $match[2];
295             }



It seems the second rule that includes a function, which is in an array, is causing this error.
Is it that this cannot be done in CI4 at all or am I just missing something?

Thanks,
Reply
#2

The closure validation rule will be added in v4.3.0.

See https://github.com/codeigniter4/CodeIgniter4/pull/6951
Reply
#3

(01-03-2023, 07:13 PM)kenjis Wrote: The closure validation rule will be added in v4.3.0.

See https://github.com/codeigniter4/CodeIgniter4/pull/6951

Great. Good to know. Thanks.
Reply
#4

The use keyword has two purposes: it tells a class to inherit a trait and it gives an alias to a namespace.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(01-03-2023, 07:13 PM)kenjis Wrote: The closure validation rule will be added in v4.3.0.

See https://github.com/codeigniter4/CodeIgniter4/pull/6951

When will version 4.3 be available?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB