Welcome Guest, Not a member yet? Register   Sign In
Form_Validation -> Repopulation not working...
#1

[eluser]pettechservices[/eluser]
I have form validation set up. It displays the proper error message at the top so I know validation is occuring, and working. However, the fields are not being repopulated. I've spent several days now trying to figure it out, reading the user guide, tutorials, etc. but I'm just stuck. For my views, they work as an include in a template. Not sure if this has anything to do with it, but I'm guessing not since the error messages still display.


Basically, they click an add New link, which calls client/client_entry. They submit the form, which goes to client/client_save, and validation runs there. If valid, saves the record and all is well (and this works). If invalid, it calls client_entry internally again and form displays (with proper error messages, but no repopulation).


Here is the code:

View (truncated):

<?php echo validation_errors(); ?>

<?php echo form_open('client/client_save');?>

<div style="float: left;padding-right: 5px; padding-bottom: 5px;">
<fieldset>
<legend>Client Information - Demographics</legend>
<ol>
<li>
<label for="clienttypeid">Client Type:</label>
<div id="clienttype">
<select tabindex="1" name="clienttypeid" id="clienttypeid">
<option value="0">Select Client Type...</option>
&lt;?php foreach ($clienttypes->result() as $ct): ?&gt;
<option value='&lt;?=$ct->cfg_clienttypes_id?&gt;'>&lt;?=$ct->cfg_clienttypes_desc?&gt;</option>
&lt;?php endforeach; ?&gt;
</select>
</div>
</li>
<li>
<label for="lastname" accesskey="f">Last name: </label>
&lt;input type="text" class="textboxlong" id="lastname" name="lastname" tabindex="2" value="&lt;?php echo set_value('lastname'); ?&gt;" /&gt;
&lt;?php /*
<br />
<span class="hint">Last name only</span>
*/ ?&gt;
</li>
<li>
<label for="firstname" accesskey="l">First name: </label>
&lt;input type="text" class="textboxlong" id="firstname" name="firstname" tabindex="3" value="&lt;?php echo set_value('firstname');?&gt;" /&gt;
</li>
<li>
<label for="address1">Address: </label>
&lt;input type="text" class="textboxlong" id="address1" name="address1" tabindex="4" value="&lt;?php echo set_value('address1');?&gt;" /&gt;
</li>
</fieldset>
</div>
&lt;/form&gt;


Controller:

&lt;?php
class Client extends Controller {

function Client()
{
parent::Controller();
$this->load->database();
$this->load->library('form_validation');
$this->load->helper('form');

if (!$this->ion_auth->logged_in())
{
//$this->session->set_flashdata('message', '');
redirect('auth/login');
}
$this->load->model('mclient');
$this->load->model('mdropdowns');
$this->load->model('memployees');
$this->load->model('mlocations');
}

function index(){
redirect('/client/client_listall');
}

function client_entry() {
$this->load->library('ion_auth');

$this->_set_rules();

$data['title'] = "Client Entry Form";
$data['headline'] = "";
$data['message'] = "";

$data['include'] = $this->config->item("companyview").'client_entry';

$data['clienttypes'] = $this->mclient->get_clienttypes(FALSE);
$data['referralsources'] = $this->mdropdowns->get_referralsources();
$data['employees'] = $this->memployees->get_employeenames();
$data['locations'] = $this->mlocations->get_locations();

$this->load->vars($data);
$this->load->view('template');
}

function client_save() {
$this->load->helper('url');
$this->load->model('mclient','',True);

$this->_set_rules();

if ($this->form_validation->run() == FALSE){
$this->client_entry();

} else {
$this->mclient->SaveEntry();
// set form input name="id"
//$this->validation->id = $id;

// set user message
$data['message'] = '<div class="success">add new person success</div>';
// load view
redirect('client','refresh');
}


}

function _set_rules(){

$this->form_validation->set_rules('clienttypeid', 'Client ID', 'trim');
$this->form_validation->set_rules('firstname', 'First Name',
'trim|min_length[3]|max_length[30]');
$this->form_validation->set_rules('lastname', 'Last Name',
'trim|required|min_length[
$this->form_validation->set_rules('address1', 'Address Line 1', 'trim');
}



Thank you!
#2

[eluser]cideveloper[/eluser]
I know this doesn't specifically address your issue but that looks way more complicated than it needs to be.

Below is a quick re-write of your controller in what I believe to be a simplified process. No need for 3 functions to do the job of one.

Code:
&lt;?php
class Client extends Controller {

    function Client(){
        parent::Controller();
        $this->load->database();
        $this->load->library(array('form_validation','ion_auth'));
        $this->load->helper(array('form','url'));

        if (!$this->ion_auth->logged_in()){
            redirect('auth/login');
        }
        $this->load->model(array('mclient','mdropdowns','memployees','mlocations'));
    }

    function index(){
        redirect('/client/client_listall');
    }

  function client_entry(){
  

      $this->form_validation->set_rules('clienttypeid', 'Client ID', 'trim');
      $this->form_validation->set_rules('firstname', 'First Name', 'trim|min_length[3]|max_length[30]');
      $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
      $this->form_validation->set_rules('address1', 'Address Line 1', 'trim');

      if ($this->form_validation->run() == FALSE){        

        $data['title'] = "Client Entry Form";
        $data['headline'] = "";
        $data['message'] = "";

        $data['include'] = $this->config->item("companyview").'client_entry';

        $data['clienttypes'] = $this->mclient->get_clienttypes(FALSE);
        $data['referralsources'] = $this->mdropdowns->get_referralsources();
        $data['employees'] = $this->memployees->get_employeenames();
        $data['locations'] = $this->mlocations->get_locations();

        $this->load->vars($data);
        $this->load->view('template');

      } else {

        $this->mclient->SaveEntry();
        $data['message'] = '<div class="success">add new person success</div>';
        redirect('client','refresh');
        
      }

  }
    
}
/* End of file client.php */
/* Location: ./system/application/controllers/client.php */

Also the $data['message'] after SaveEntry doesnt look like it does anything since you are redirecting the page. Probably should be using flashdata.

And why does the index redirect to client_listall. Why doesnt index just do what client_listall does.

Remember to change
Code:
&lt;?php echo form_open(‘client/client_save’);?&gt;
to
Code:
&lt;?php echo form_open(‘client/client_entry’);?&gt;
in your view
#3

[eluser]pettechservices[/eluser]
First, thank you so very much! The rewrite actually worked well and now repopulates. I could just have index do client_listall without calling it, but I just liked having it seperated for readability.

Since I'm going to use the same form (view) for adding and editing, I can just add additional code into client_entry to display the pre-populated information, correct? And some type of flag to update vs insert?

Thanks,
Chris




Theme © iAndrew 2016 - Forum software by © MyBB