Welcome Guest, Not a member yet? Register   Sign In
issue with re-populating the field values after form validation
#1

[eluser]Mithun[/eluser]
Hi All,
I have an issue with re-populating the field values on the add view once the user submits the form and the validation fails,
Problem is i can get values from set_value function in the view
Here is my controller
Code:
<?php
class Locations extends MY_Controller {
    
    function Locations()
    {
        parent::MY_Controller();
    }
    
    function add()
    {
        if(empty($_POST))
        {
            //$data['customers'] = $this->db->get('customers');
            $data = array();
            $this->template->write_view('content','locations/add',$data);    

        }
        else
        {
            $this->load->library('validation');
            $rules['name']    = "trim|required|min_length[1]|max_length[12]|xss_clean";
            $rules['assigned_id']    = "trim|required|";
            $this->validation->set_rules($rules);
            /*
            $data = array(
                'name' => $_POST['name'],
                'address' => $_POST['address'],
                'assigned_id' => $_POST['assigned_id'],
            );
            */
            if ($this->validation->run()){
                $this->db->insert('locations',$data);
                redirect('/locations/index/');
            } else {
                //print_r($data);    
                $this->template->write_view('content','locations/add',$data);    
                //$this->template->write_view('content','locations/add');    
            }
        }
        $this->template->render();
    }
    
    
}
here is my view
Code:
<?php
    $name = array(
        'id'=>'location_add_name',
        'name' => 'name',
        'value' => set_value('name')
    );
    
    $address = array(
        'id' => 'location_add_address',
        'name' => 'address',
        'value' => set_value('address')
    );
    
    $assigned_id = array(
        'id' => 'location_add_address_assigned_id',
        'name' => 'assigned_id',
        'value' => set_value('assigned_id')
    );
    print_r($name);
?>
<div id='locations_add' class="add">
    &lt;?php echo form_open('locations/add');?&gt;
    &lt;?php echo validation_errors(); ?&gt;
    <div class='field_wrapper'>
        <div class='field_label'>
            &lt;?php echo form_label('Name', $name['id']);?&gt;
        </div>
        <div class='field_input'>
            &lt;?php echo form_input($name);?&gt;
        </div>
    </div>
    <div class='field_wrapper'>
        <div class='field_label'>
            Address
        </div>
        <div  class='field_input'>
            &lt;?php echo form_input($address);?&gt;
        </div>
    </div>
    <div class='field_wrapper'>    
        <div class='field_label'>
            Assigned User
        </div>
        <div class='field_input'>
            &lt;?php echo form_input($assigned_id);?&gt;
        </div>
    </div>
    <div class='form_submit'>
        &lt;?php echo form_submit('submit','Add');?&gt;
    </div>
    &lt;?php echo form_close();?&gt;
</div>

And I'm not getting anything from set_value('name') ;-(
#2

[eluser]rogierb[/eluser]
I think the validation library provides the values for get_value(). So it need to be loaded.

So you could try something like
Code:
function add()
{
     $data = array();

     $this->load->library('validation');
     $rules['name']    = "trim|required|min_length[1]|max_length[12]|xss_clean";
     $rules['assigned_id']    = "trim|required|";
     $this->validation->set_rules($rules);

     if ($this->validation->run()){
         $this->db->insert('locations',$data);
         redirect('/locations/index/');
     } else {

         $this->template->write_view('content','locations/add',$data);

     }
     $this->template->render();
}
#3

[eluser]Mithun[/eluser]
Validation library is already loaded in autoload.php
Code:
$autoload['libraries'] = array('database', 'session','template','DX_Auth', 'Form_validation', 'validation');
$autoload['helper'] = array('html', 'url','form');

Controller is
Code:
function add()
{
     $data = array();

     $this->load->library('validation');
     $rules['name']    = "trim|required|min_length[1]|max_length[12]|xss_clean";
     $rules['assigned_id']    = "trim|required|";
     $this->validation->set_rules($rules);

     if ($this->validation->run()){
         $this->db->insert('locations',$data);
         redirect('/locations/index/');
     } else {

         $this->template->write_view('content','locations/add',$data);

     }
     $this->template->render();
}

view is
Code:
&lt;?php
    $name = array(
        'id'=>'location_add_name',
        'name' => 'name',
        'value' => set_value('name')
    );
    
    $address = array(
        'id' => 'location_add_address',
        'name' => 'address',
        'value' => set_value('address')
    );
    
    $assigned_id = array(
        'id' => 'location_add_address_assigned_id',
        'name' => 'assigned_id',
        'value' => set_value('assigned_id')
    );
    print_r($name);
?&gt;

and the output of the print_r($name) is
Code:
Array
(
    [id] =&gt; location_add_name
    [name] =&gt; name
    [value] =&gt;

Value of name is not repopulated :-(p
#4

[eluser]rogierb[/eluser]
Ah, you are using the validation library, which is deprecated. If you want to use set_value(), you have to use form_validation.
#5

[eluser]Mithun[/eluser]
Sure I'm using the same
Code:
$autoload['libraries'] = array('database', 'session','template','DX_Auth', 'Form_validation', 'validation');
#6

[eluser]danmontgomery[/eluser]
[quote author="Mithun" date="1264109102"]Sure I'm using the same
Code:
$autoload['libraries'] = array('database', 'session','template','DX_Auth', 'Form_validation', 'validation');
[/quote]

You're loading form_validation, but you're using validation.
#7

[eluser]Mithun[/eluser]
That's correct, I removed all reference to deprecated validation class and it worked
Code:
function add()
{
     $data = array();
    
     $this->load->library('form_validation');

     $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[1]|max_length[12]|xss_clean');
     $this->form_validation->set_rules('assigned_id', 'AssignedId', 'trim|required');
     $this->form_validation->set_rules('address', 'Address', 'trim');

     if ($this->form_validation->run()){
         $this->db->insert('locations',$data);
         redirect('/locations/index/');
     } else {
         $this->template->write_view('content','locations/add',$data);

     }
     $this->template->render();
}

Thanks rogierb, Thanks noctrum.




Theme © iAndrew 2016 - Forum software by © MyBB