[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">
<?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') ;-(
[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();
}
[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: <?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
Code: Array
(
[id] => location_add_name
[name] => name
[value] =>
Value of name is not repopulated :-(p
[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.
[eluser]Mithun[/eluser]
Sure I'm using the same
Code: $autoload['libraries'] = array('database', 'session','template','DX_Auth', 'Form_validation', 'validation');
[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.
[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.
|