CodeIgniter Forums

Full Version: CI4 equivalent of $this->form_validation->set_data($data);
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Hi, you do it like this.

PHP Code:
$validation =  \Config\Services::validation();
$validation->setRules($rules$messages);
$validation->run($data); 
(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