CodeIgniter 3 Form Validation

I am doing my first application in CodeIgniter 3. I have a rather long registration form for which I listed all the validation rules in my create() method of the Registrations controller. If a rule fails, the user is redirected to the registration form, hopefully to show the error message at the top.

Problem:

  1. Here is my code for redirecting:

    if ($this->form_validation->run(‘validate_registration’) == FALSE) {
    $data = array(
    ‘errors’ => validation_errors()
    );
    $this->session->set_flashdata($data);
    redirect(‘registrations’, $data);
    }

  2. It takes me to the index() method of the Registrations controller.

public function index() 
    {
        $data['user_id'] = $_SESSION['user_id'];
        $data = array(
            'main_content' => 'registration', 
            'page_title' => 'Restore CRM - Registration',
            'body_class' => 'registration',
            'current' => '',
            'case_status' => $this->registration_model->get_case_status(),
            'churches' => $this->registration_model->get_churches(),
            'careplanners' => $this->user_model->get_careplanners(),
            'coordinators' => $this->user_model->get_coordinators(),
            'inquiry_types' => $this->registration_model->get_inquiry_types(),
            'regions' => $this->registration_model->get_regions()
        );        
        
        if (isset($_SESSION['user_role']->role_id)) {
                $data['role_id'] = $_SESSION['user_role']->role_id;
            } else {
                $data['role_id'] = '';
            }
        
        $this->load->view('layouts/main', $data);
                
    }
  1. The error messages won’t show up because I think the $data array is overwritten by the $data array in the index() method. Many of the elements of this array are bringing values in from the database to populate form dropdowns. The first time the form is generated, the $data array from the form validation hasn’t been built yet.

How do I get those error messages back to the registration form in this situation?

I think I fixed my own problem. I moved the form_validation and the call to the model for inserting data into the index() method, instead of having a separate create() method.

However, any advice from developers experienced with CodeIgniter 3 will be greatly appreciated.

Cant you make a validate method and return the validated data ?
If is validated pass to the method where it should go ?

Note I don’t use CI 3 can you pm me how it is ?

CodeIgniter 3 has a form_validation library that I am using. For learning purposes, I’m trying to do everything the CodeIgniter way, even though I could use straight php.

I spent several months learning Laravel 5 and CodeIgniter 3 and really like both frameworks. I decided to use CodeIgniter 3 for this application because I don’t really have the time to become comfortable with all the extras that Laravel 5 uses such as Composer, Grunt, etc. But when I’m finished, I’m going to return to my Laravel 5 learning.

I found that CodeIgniter 3 is a little short on resources and community support.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.