• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple form validation problem

#11
[eluser]Matalina[/eluser]
I am validating two forms on one page here is my controller code. Basically It's one form with two submit buttons.

Code:
public function edit_user($user_id = '') {
    $this->load->model('Auth_model');
    if($user_id != '') {
      $user = $this->Auth_model->get_user($user_id);
    }
    else {
      $user = $this->Auth_model->get_user($this->input->post('user_id'));
    }

    if($this->input->post('edit_user')) {

      $this->form_validation->set_rules('username', 'Username', 'required');
      $this->form_validation->set_rules('first_name', 'First Name', 'required');
      $this->form_validation->set_rules('last_name', 'Last Name', 'required');
      $this->form_validation->set_rules('store_id', 'Store/District', 'required');
      $this->form_validation->set_rules('route', 'Route', 'integer');

      $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

      if ($this->form_validation->run()) {
        $posts = $this->input->post();
        $check = $this->Auth_model->edit_user($posts);

        if($check) {
          $this->session->set_flashdata('success','<div class="success">User info was changed</div>');
          redirect('maintenance/users');
        }
        else {
          $this->session->set_flashdata('error','<div class="error">User info was not changed.</div>');
          redirect('maintenance/users');
        }
      }
    }

    if($this->input->post('change_password')) {
      $this->form_validation->set_rules('password', 'Password', 'required');
      $this->form_validation->set_rules('confirm', 'Confirm Password', 'required|matches[password]');

      $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

      if ($this->form_validation->run()) {
        $check = $this->Auth_model->reset_password($this->input->post('user_id'),$this->input->post('password'));

        if($check) {
          $this->session->set_flashdata('success','<div class="success">User info was changed</div>');
          redirect('maintenance/users');
        }
        else {
          $this->session->set_flashdata('error','<div class="error">User info was not changed.</div>');
          redirect('maintenance/users');
        }
      }
    }

    $this->load->model('Stores_model');
    $this->load->helper('general');
    $districts_dd = make_dropdown($this->Stores_model->get_districts(),'district_name','district_name');
    $stores_dd = make_dropdown($this->Stores_model->get_all_stores(),'STID','STID');
    $data['stores'] = array('Home Office' => array('999' => '999'),'Districts' => $districts_dd, 'Stores' => $stores_dd);
    $data['user'] = $user;
    $data['title'] = 'Edit User';
    $this->template->load('templates/two_column_layout','maintenance/edit_user',$data);
  }

And here is the form code:

Code:
&lt;?php
  echo form_open('maintenance/edit_user');
  echo form_hidden('user_id',$user['user_id']);
  echo form_fieldset('Edit User');

  echo form_label('Username');
  echo form_input('username',set_value('username',$user['username']));
  echo form_error('username');
  echo '<br/>';

  echo form_label('First Name');
  echo form_input('first_name',set_value('first_name',$user['first_name']));
  echo form_error('first_name');
  echo '<br/>';

  echo form_label('Last Name');
  echo form_input('last_name',set_value('last_name',$user['last_name']));
  echo form_error('last_name');
  echo '<br/>';

  echo form_label('Email');
  echo form_input('email',set_value('email'));
  echo form_error('email');
  echo '<br/>';

  echo form_label('Store/District');
  echo form_dropdown('store_id',$stores,set_value('store_id',$user['store_id']));
  echo form_error('store_id');
  echo '<br/>';

  echo form_label('Route');
  echo form_input('route',set_value('route',$user['route']));
  echo form_error('route');
  echo '<br/>';

  echo form_submit('edit_user','Edit User');
  echo form_fieldset_close();

  echo form_fieldset('Change Password');

  echo form_label('Password');
  echo form_password('password');
  echo form_error('password');
  echo '<br/>';

  echo form_label('Confirm Password');
  echo form_password('confirm');
  echo form_error('confirm');
  echo '<br/>';

  echo form_submit('change_password','Change Password');
  echo form_fieldset_close();
  echo form_close();
?&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.