Welcome Guest, Not a member yet? Register   Sign In
controller function form validation
#1

[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>


Messages In This Thread
controller function form validation - by El Forum - 02-13-2012, 08:56 PM
controller function form validation - by El Forum - 02-14-2012, 10:36 AM
controller function form validation - by El Forum - 02-14-2012, 11:51 AM
controller function form validation - by El Forum - 02-14-2012, 12:16 PM
controller function form validation - by El Forum - 02-16-2012, 10:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB