I am using codeigniter and have a form for add transfers. I have written validation rules in the controller but when the form submitted the errors message appear but the form forget the values which was entered by the user.
I try to use set_value and it works but sometimes I want to make if statements and I think it mus be in the controller not in the view. Is there a way to make the set value in the controller and return it in $data to the view
view
<?php $type_val= set_value('type'); ?>
<select class="form-control show-tick" id="type" name="type">
<option value="<?=set_value('type');?>" ><?=($type_val == 1) ? 'Exit' : 'Come';?></option>
<option value="1">Exit</option>
<option value="2">COME</option>
</select>
controller
public function add(){
if($this->input->post('submit')){
$rules = $this->get_rules_validation();
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == FALSE) {
$data['title'] = 'Add Transfer';
$data['view'] = 'admin/transfer/transfer_add';
$data['validation_error'] = true;
$this->load->view('layout', $data);
}
else{
$data = array(
'type' => $this->input->post('type'),
);
$data = $this->security->xss_clean($data);
$result = $this->transfer_model->add_transfer($data);
if($result){
$this->session->set_flashdata('msg', 'Transfer is Added Successfully!');
redirect(base_url('admin/transfer'));
}
else{
$this->session->set_flashdata('msg', 'Transfer Cant Be added!');
redirect(base_url('admin/transfer'));
}
}
}
else{
$data['title'] = 'Add Transfer';
$data['view'] = 'admin/transfer/transfer_add';
$this->load->view('layout', $data);
}
}
@Samira, There is no such a way to set_value
in the controller. However, you can check the posted value and mark it as selected and pass in the $data
but you have to check again that value to show an option as selection in the view that will make no sense.
And you should use set_select
instead of set_value
.
For example
<select class="form-control show-tick" id="type" name="type">
<option value="1" <?php echo set_select('type', '1'); ?>>Exit</option>
<option value="2" <?php echo set_select('type', '2'); ?>>COME</option>
</select>
You can see full documentation here. Set select codeigniter
One more thing, you don't need to load view twice. You can load it only once.
$this->form_validation->run()
will execute only when your form is submitted.
i have put the validation in a function instead
public function get_rules_validation(){
$rules = array(
array(
'field' => 'type',
'label' => 'Type',
'rules' => 'is_natural_no_zero',
'errors' => array(
'is_natural_no_zero' => 'You must provide a %s.',
),
);
return $rules;
}
add
public function add(){
if($this->input->post('submit')){
$rules = $this->get_rules_validation();
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == FALSE) {
$data['title'] = 'Add Transfer';
$data['view'] = 'admin/transfer/transfer_add';
$data['validation_error'] = true;
$this->load->view('layout', $data);
}
else{
$data = array(
'created_at' => date('Y-m-d : h:m:s'),
);
$data = $this->security->xss_clean($data);
$result = $this->transfer_model->add_transfer($data);
if($result){
$this->session->set_flashdata('msg', 'Transfer is Added Successfully!');
redirect(base_url('admin/transfer'));
}
else{
$this->session->set_flashdata('msg', 'Transfer Cant Be added!');
redirect(base_url('admin/transfer'));
}
}
}
else{
$data['title'] = 'Add Transfer';
$data['view'] = 'admin/transfer/transfer_add';
$this->load->view('layout', $data);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.