Search code examples
validationcodeigniter-4

Codeigniter 4 validation errors not showing


I have some issue with my validation error message for Codeigniter 4, the validation is working but the error messages are not showing, I don't know what went wrong. Below are my code :

Controller

public function register()
    {
        $data = [];
        helper(['form']);
        
        // To add in is_unique
        
        if($this->request->getMethod() == 'post'){
            //validations
            $rules = [
                'username' => 'required',
                'email' => 'required|valid_email',
                'firstname' => 'required',
                'lastname' => 'required',
                'dob' => 'required',
                'country' => 'required',
                'contact' => 'required',
                'password' => 'required'
            ];
            
            if(!$this->validate($rules)){
                $data['validation'] = $this->validator;
            }else{
                //store information into database
                
                
            }
        }
        
        echo view('templates/header', $data);
        echo view('account/register');
        echo view('templates/footer');
    }

View

<?php if(isset($validation)): ?>
            <div class="form-group col-12 col-md-12">
                <div class="alert alert-danger" role="alert">
                    <?php $validation->listErrors() ?>
                </div>
            </div>
            <?php endif; ?>

Thanks in advance for the help guys!


Solution

  • Ok. I watched some videos and managed to get it working by changing

    <?php $validation->listErrors() ?> 
    to 
    <?php echo \Config\Services::validation()->listErrors() ?>