Problem inserting multiple fields to database - El Forum - 05-06-2009
[eluser]opel[/eluser]
I have a form with 3 fields, for every field that has all 3 fields completed I want to insert a unique record for each.
The 2 problems I have are :
1. How do I validate the form so that each insert has its own validation ?
2. If I only enter the details for 1 of the fields then then the insert doesn't work, (offset issue), however everything works if I complete all the fields?
This will only be used for an insert functions .
My Form fields area set as so (repeated 3 times):
Code: echo form_fieldset('Method 3');
echo "<div class=\"span-6\"><p><label for='navigation_name'>Name : </label><br/>";
$data = array('name'=>'navigation_name[]','id'=>'navigation_name','class'=>'', 'value'=>$navigation_name );
echo form_input($data) ."</p></div>";
echo "<div class=\"span-6\"><p><label for='navigation_method'>Method : </label><br/>";
$data = array('name'=>'navigation_method[]','id'=>'navigation_method','class'=>'', 'value'=>$navigation_method );
echo form_input($data) ."</p></div>";
echo "<div class=\"span-2\"><p><label for='navigation_order'>Order : </label>";
$data = array('name'=>'navigation_order[]','id'=>'navigation_order','size'=>'3', 'value'=>$navigation_order );
echo form_input($data) ."</p></div>";
echo "<div class=\"span-2 last\"><br /><label for='navigation_show'>Show : </label>";
$data = array('name'=>'navigation_show[]','id'=>'navigation_show','class'=>'', 'value'=>$navigation_show, set_checkbox('navigation_show', '1', TRUE));
echo form_checkbox($data) ."</p></div>";
echo form_fieldset_close(); */
And my add method
Code: function add()
{
//set validation rules
$this->form_validation->set_rules('navigation_name', 'name', 'required|max_length[15]|xss_clean ');
$this->form_validation->set_rules('navigation_controller', 'controller', 'trim|required|min_length[50]|xss_clean');
$this->form_validation->set_rules('navigation_section', 'section', 'required|max_length[50]|xss_clean ');
$this->form_validation->set_rules('navigation_method', 'method', 'trim|required|min_length[50]|xss_clean');
$this->form_validation->set_rules('navigation_order', 'order', 'trim|required|max_length[4]|xss_clean');
$this->form_validation->set_rules('navigation_show', 'live', 'trim|integer');
// set custom delimiters
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$formvalue = $this->crud->getValues($this->table, $id=NULL);
$formvalue['navigation_show'] = 1;
if (!isset($_POST['submit']))
{
//set page title
$formvalue['pageTitle'] = "Add Navigation Items";
$formvalue['sections'] = $this->config->item('sections');
$formvalue['description'] = "Add a ".$this->page." item by filling the form off below";
$formvalue['button'] = "Add Navigation Item";
//load the view
$this->template->load('admin', 'admin/navigation_form', $formvalue);
} // end of form processing
else // populate database with the form values
{
foreach($_POST['navigation_name'] as $row => $name)
{
$this->db->set("navigation_controller", $_POST['navigation_controller']);
$this->db->set("navigation_section", $_POST['navigation_section']);
$this->db->set("navigation_name", $_POST['navigation_name'][$row]);
$this->db->set("navigation_method",$_POST['navigation_method'][$row]);
$this->db->set("navigation_order", $_POST['navigation_order'][$row], FALSE);
$this->db->set("navigation_show", $_POST['navigation_show'][$row], FALSE);
//add values to the Database
$this->db->insert($this->table);
}
//set success message
$this->session->set_flashdata('message', 'Successfully Added !');
redirect('admin/'.$this->table.'', 'refresh');
}
}
Any help or suggestions appreaciated
Problem inserting multiple fields to database - El Forum - 05-06-2009
[eluser]Thorpe Obazee[/eluser]
I'd say you'd have to have 3 validation rule sets and use
Code: if ($this->form_validation->run('first_input_rule_set') == TRUE)
{
do stuff
}
if ($this->form_validation->run('second_input_rule_set') == TRUE)
{
do stuff
}
if ($this->form_validation->run('third_input_rule_set') == TRUE)
{
do stuff
}
You also need to modify your view to have different fieldnames instead of fieldname[]
One problem is that I don't know if two or more form validation rule sets could run at one request. Haven't tried it though.
Problem inserting multiple fields to database - El Forum - 05-07-2009
[eluser]opel[/eluser]
Ok I'll give that a shot
|