CodeIgniter Forums
controller function form validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: controller function form validation (/thread-49275.html)



controller function form validation - El Forum - 02-13-2012

[eluser]jzmwebdevelopement[/eluser]
Hey,

I have the controller below and I would like it do the following things:

Show an error if the username is taken -> currently just submits as normal even through the specific user is in the database.

Populate with data if there was an error -> currently populates with the imported data if correct

Keep active state of the selected drop down menu item

Controller:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class createUser extends CI_Controller {


public function index()
{
  //Form Validation prep making sure its all clean
  
  $this->form_validation->set_rules('userName', 'User Name', 'trim|required|is_unique[users.userName]|xss_clean');
  $this->form_validation->set_rules('userPassword', 'User Password', 'trim|required|xss_clean|sha1');
  $this->form_validation->set_rules('userFirstName', 'First Name', 'trim|required|xss_clean');
  $this->form_validation->set_rules('userLastName', 'Last Name', 'trim|required|xss_clean');
  $this->form_validation->set_rules('userEmail', 'E-Mail', 'trim|required|xss_clean');
  $this->form_validation->set_rules('userGroup', 'User Group', 'trim|required|xss_clean');
  $this->form_validation->set_rules('userActive', 'User Active', 'trim|required|xss_clean');
  
  //If form validation fails load previous page with errors else do the job and insert data into db
  
  if($this->form_validation->run() == FALSE)
  {
   $data['success'] = "";
  }else{
   $username = $this->input->post('userName');
   $password = $this->input->post('userPassword');
   $firstname = $this->input->post('userFirstName');
   $lastname = $this->input->post('userLastName');
   $email = $this->input->post('userEmail');
   $group = $this->input->post('userGroup');
   $active = $this->input->post('userActive');
  
   $passwordHash = $this->encrypt->sha1($password); // Lets encrypt the password why sha1?  MD5 is for tossers
  
   // If the data is correct follow through with db insert
  
   if($this->users_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$group,$active))
   {
    $data['success'] = TRUE;
    
   }
  
  }
  $data['companyName'] = $this->core_model->companyName();
  $data['pageTitle'] = "Create User";
  $this->load->view('admin/assets/header', $data);
  $this->load->view('admin/createUser', $data);
  $this->load->view('admin/assets/footer');
}
}

/* End of file login.php */
/* Location: ./application/controllers/admin/createUser.php */

View:

Code:
<h1>&lt;?php echo $companyName; echo nbs(1);?&gt; - &lt;?php echo $pageTitle; ?&gt;</h1>



&lt;?php
if($success == TRUE) {
echo '<section id = "validation">Page Updated</section>';
}
?&gt;
<p>Error: &lt;?php echo validation_errors();?&gt;</p>
<div class="formContent">
  &lt;form action="createUser" method="post"&gt;
   <fieldset class="control-group">
    <label for="userName">User Name:</label>&lt;input type="text" id="userName" name="userName"  value="&lt;?php echo set_value('userName'); ?&gt;" placeholder="User Name"&gt;
    <label for="userPassword">User Password:</label>&lt;input type="password" id="userPassword" name="userPassword" value="&lt;?php echo set_value('userPassword'); ?&gt;" placeholder="User Password"&gt;
    <label for="userFirstName">First Name:</label>&lt;input type="text" id="userFirstName" name="userFirstName" value="&lt;?php echo set_value('userFirstName'); ?&gt;" placeholder="First Name"&gt;
    <label for="userLastName">Last Name:</label>&lt;input type="text" id="userLastName" name="userLastName" placeholder="Last Name"&gt;
    <label for="userEmail">E-Mail:</label> &lt;input type="text" id="userEmail" name="userEmail"  placeholder="Admin E-mail"&gt;
    <label for="userGroup"> User Group:</label>
     <select name="userGroup" id="userGroup">
      <option value="select">Please Select</option>
      <option value="admin">Admin</option>
      <option value="user">User</option>
     </select>
    <label for="userActive"> User Active:</label>
      <select name="userActive" id="userActive">
       <option value="select">Please Select</option>
       <option value="yes">Yes</option>
       <option value="no">No</option>
      </select>
    <div>
     <button type="submit" class="btn-primary">Create</button>
    </div>
    </fieldset>
  &lt;/form&gt;
</div>



controller function form validation - El Forum - 02-14-2012

[eluser]Jan_1[/eluser]
I don't see that you do
Code:
$this->load->library('form_validation');
Is that your problem or what is your question?


controller function form validation - El Forum - 02-14-2012

[eluser]jzmwebdevelopement[/eluser]
[quote author="Jan from Hamburg" date="1329240990"]I don't see that you do
Code:
$this->load->library('form_validation');
Is that your problem or what is your question?[/quote]

My Question is:

Show an error if the username is taken -> currently just submits as normal even through the specific user is in the database.

Populate with data if there was an error -> currently populates with the imported data if correct

Keep active state of the selected drop down menu item

I have
Code:
$this->load->library('form_validation');
on auto load


controller function form validation - El Forum - 02-14-2012

[eluser]Jan_1[/eluser]
[quote author="jzmwebdevelopement" date="1329245489"]
My Question is:
Show an error if the username is taken -> currently just submits as normal even through the specific user is in the database.[/quote]
Sorry, I don't know. May be its because of your Success-Check of users_model->createUser. Maybe have a try with a simple callback_function instead of is_unique. Maybe it's the model. I don't know.
[quote author="jzmwebdevelopement" date="1329245489"]
Populate with data if there was an error -> currently populates with the imported data if correct
[/quote]
What do you mean with imported data? Should be what was filled in the form fields, isn'it? And you reopen the form field directly after insert by the same function. If you do a redirect('createUser',refresh) you don't get filled form fields after correct insert.


controller function form validation - El Forum - 02-16-2012

[eluser]ppwalks[/eluser]
Code:
function something_exists($something)
{
  $query = $this->db->get_where('something', array('something' => $something));
  
  if($query->num_rows() > 0)
  {
   $this->form_validation->set_message('something_exists', 'The %s already exists in our database, please use a different one.');
   return FALSE;
  }
  
  $query->free_result();
  
  return TRUE;
}

Something like this should do it I think, modify for your own purposes...