(Level 3) Client + Model

At last! In this Level 3 Validation, we don't really have to do anything new. Our website is already secure because we do not only rely on client-side validations to make sure that the data we put into our database is valid. However, right now our controller has too many lines of code! Remember, the controller just delegates. The controller is doing too much when it should be delegating its work to the Model and the View. It makes more sense to have the Model:

  • deal with the validations because Models are the gatekeepers to the database
  • be responsible for making sure that the data that is about to go into the database is valid.

All we will be doing in this tab is moving our validation code from the controller to the model so that we will be able to uphold the general rule of thumb: skinny controller and fat model. Go ahead and check out the level_three branch of the repository you cloned before by running the following command:

git checkout level_three

Now, if you run the application on localhost you will realize that nothing has changed. The core functionality of the application is the same; we just moved some of the code to a different part of the application. Here is how the controller looks now:

``` <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Users extends CI_Controller { public function new_user() { $this->load->view('users/new'); } public function create() { $this->load->model('User'); $result = $this->User->validate(); if($result == "valid") { $id = $this->User->create($this->input->post()); $success[] = 'Welcome! Registration was successful!'; $this->session->set_flashdata('success', $success); redirect('/users/show/' . $id); } else { $errors = array...