I have a view containing a form. with input fields and radio buttons. This form needs to be populated with the data from database, so as to use it as an data editing form.
I have used the set_value() function for the form validation where the view is called from the same controller after the post. But how to use set_value function with out a POST? i.e. simply populate the values in a form in the view that's loaded. with say, an Array or object from database.
The set_value function just sets the value, the only benefit to using it is it simplifies setting the value to an already submitted value when redisplaying the form or showing a default value when the form has yet to be submitted.
Create a view file myform. php and save the below code it in application/views/myform. php. This page will display form where user can submit his name and we will validate this page to ensure that it should not be empty while submitting.
CodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();
You can use it like this
set_value('myfield', isset($databaseData['myfield']) ? $databaseData['myfield'] : '');
$databaseData
will contain the data from the database which you have loaded in the controller and passed to view
$data['databaseData']= $this->my_model->find($parameters);
$this->load->view('myformview',$data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With