title: codeigniter CMS form validation
body: <p>Firstly Im new to CodeIgniter and MVC.</p>
  
  <p>I am Creating a CMS and coudln't decide which route to take with do I have two applications (front end/CMS) or just create the admin as a controller. I opted for one application and creating the admin via a Controller.</p>
  
  <p>Doing it this way I have ran into a problem with form validation where if it doesn't validate I cant load the form I have to redirect which then means it wont repopulate the unvalidated fields. I use a variable in the 3rd URI segment to determine whether to display a form for inserting a new record, a populated form for editing a record, or a tabled list of all records.</p>
  
  <p><strong>The form posts to /admin/videos/save</strong></p>
  
  <pre><code>function videos()
  {
      if (!$this-&gt;tank_auth-&gt;is_logged_in()) {
          redirect('/auth/login/');
      } else {
  
          $this-&gt;load-&gt;model('videos_model');
  
          $data['section'] = "Videos";
          $data['area']    = "Videos";
          $data['mode']    = $this-&gt;uri-&gt;segment(3, 'create');
          $data['user_id']  = $this-&gt;tank_auth-&gt;get_user_id();
          $data['username'] = $this-&gt;tank_auth-&gt;get_username();
  
          if ($data['mode'] == 'edit') {
  
              $data['ID']     = $this-&gt;uri-&gt;segment(4);
              $data['videos'] = $this-&gt;videos_model-&gt;get_videos($data['ID']);
  
          } elseif ($data['mode'] == 'list') {
  
              if ($this-&gt;uri-&gt;segment(4)) {
                  $data['filter'] = $this-&gt;uri-&gt;segment(4);
  
                  $data['videos'] = $this-&gt;videos_model-&gt;get_filtered_videos($data['filter']);
              } else {
                  $data['videos'] = $this-&gt;videos_model-&gt;get_filtered_videos();
              }
          } elseif ($data['mode'] == 'save') {
              $this-&gt;load-&gt;helper('form');
              $this-&gt;load-&gt;library('form_validation');
  
              $this-&gt;form_validation-&gt;set_rules('videoTitle', 'Title', 'required');
              $this-&gt;form_validation-&gt;set_rules('Code', 'Youtube Code', 'required');
  
              if ($this-&gt;form_validation-&gt;run() === FALSE) {
                  redirect('/admin/videos');
              } else {
                  $this-&gt;videos_model-&gt;set_videos();
                  redirect('/admin/videos/list');
              }
          }
  
          if ($data['mode'] != "create" &amp;&amp; empty($data['videos'])) {
              show_404();
          }
  
  
  
          $this-&gt;load-&gt;view('admin/templates/head', $data);
          $this-&gt;load-&gt;view('admin/templates/body_navbar', $data);
          $this-&gt;load-&gt;view('admin/videos', $data);
          $this-&gt;load-&gt;view('admin/templates/footer', $data);
      }
  }
  </code></pre>
  
  <p>Am I setting about this the wrong way, Should I use two application folders or have 3 controllers for editing/inserting/viewing all. Or is there a solution to my current setup?</p>