Part of the EllisLab Network
   
 
issue with re-populating the field values after form validation
Posted: 21 January 2010 09:39 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  23
Joined  01-12-2010

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

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

<?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">
    
<?php echo form_open('locations/add');?>
    <?php 
echo validation_errors(); ?>
    
<div class='field_wrapper'>
        <
div class='field_label'>
            
<?php echo form_label('Name'$name['id']);?>
        
</div>
        <
div class='field_input'>
            
<?php echo form_input($name);?>
        
</div>
    </
div>
    <
div class='field_wrapper'>
        <
div class='field_label'>
            
Address
        
</div>
        <
div  class='field_input'>
            
<?php echo form_input($address);?>
        
</div>
    </
div>
    <
div class='field_wrapper'>    
        <
div class='field_label'>
            
Assigned User
        
</div>
        <
div class='field_input'>
            
<?php echo form_input($assigned_id);?>
        
</div>
    </
div>
    <
div class='form_submit'>
        
<?php echo form_submit('submit','Add');?>
    
</div>
    
<?php echo form_close();?>
</div

And I’m not getting anything from set_value(‘name’) ;-(

 Signature 

Mithun P Sreedharan

Profile
 
 
Posted: 21 January 2010 09:47 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  686
Joined  05-24-2007

I think the validation library provides the values for get_value(). So it need to be loaded.

So you could try something like

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();
 
 Signature 

The art of managing is to explain tomorrow why yesterdays solution doesn’t work today.
livecodes.eu

Profile
 
 
Posted: 21 January 2010 09:54 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  23
Joined  01-12-2010

Validation library is already loaded in autoload.php

$autoload['libraries'= array('database''session','template','DX_Auth''Form_validation''validation');
$autoload['helper'= array('html''url','form'); 

Controller is

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

<?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);
?> 

and the output of the print_r($name) is

Array
(
    
[id] => location_add_name
    [name] 
=> name
    [value] 
=> 

Value of name is not repopulated :-(p

 Signature 

Mithun P Sreedharan

Profile
 
 
Posted: 21 January 2010 10:28 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  686
Joined  05-24-2007

Ah, you are using the validation library, which is deprecated. If you want to use set_value(), you have to use form_validation.

 Signature 

The art of managing is to explain tomorrow why yesterdays solution doesn’t work today.
livecodes.eu

Profile
 
 
Posted: 21 January 2010 11:25 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  23
Joined  01-12-2010

Sure I’m using the same

$autoload['libraries'= array('database''session','template','DX_Auth''Form_validation''validation'); 
 Signature 

Mithun P Sreedharan

Profile
 
 
Posted: 21 January 2010 11:27 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1835
Joined  12-08-2009
Mithun - 21 January 2010 04:25 PM

Sure I’m using the same

$autoload['libraries'= array('database''session','template','DX_Auth''Form_validation''validation'); 

You’re loading form_validation, but you’re using validation.

 Signature 

@basdflasjk | BitAuth: Authentication and Role-based Permissions | Session Library Replacement


Please read the User Guide! (Upgrading from a previous version?)

Profile
 
 
Posted: 21 January 2010 11:51 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  23
Joined  01-12-2010

That’s correct, I removed all reference to deprecated validation class and it worked

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.

 Signature 

Mithun P Sreedharan

Profile