• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4 equivalent of $this->form_validation->set_data($data);

#1
Hello,

I need to validate a value outside of $_POST data. In CI3 I could easily manually assign data before running validation:

PHP Code:
$data = array(
        'username' => 'johndoe',
        'password' => 'mypassword',
        'passconf' => 'mypassword'
);

$this->form_validation->set_data($data); 


But it seems this feature is not exist on CI4.

How to do this?

Thanks.
Reply

#2
Hi, you do it like this.

PHP Code:
$validation =  \Config\Services::validation();
$validation->setRules($rules$messages);
$validation->run($data); 
Reply

#3
(08-08-2020, 12:09 PM)jreklund Wrote: Hi, you do it like this.

PHP Code:
$validation =  \Config\Services::validation();
$validation->setRules($rules$messages);
$validation->run($data); 

It worked, thanks.

Reference: https://codeigniter4.github.io/userguide...r-messages

Final code:
PHP Code:
            $data = array(
                'variable1' => $some_value,
            );

            $rules = [
                'variable1' => 'required|min_length[12]|max_length[15]',
            ];

            $messages = [
                'variable1' => [
                    'required' => 'Variable 1 required.',
                    'min_length' => 'Minimum 12 characters.',
                    'max_length' => 'Maximum 15 characters.',
                ],
            ];

            $validation = \Config\Services::validation();
            $validation->setRules($rules$messages);

            if (!$validation->run($data)) {
                
// validation failed
                $errors $validation->getErrors();
            } else {
                
// validation success
            

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.