CodeIgniter Forums
Validation problem - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Validation problem (/thread-39803.html)

Validation problem - El Forum - 03-21-2011

[eluser]Frank Rocco[/eluser]
Hello, the code below produces a field is required message when the form is first displayed.
I cannot seem to fix this.

Does the code look ok?

$Item_description = array(
              'name'        => 'Item_description',
              'id'          => 'Item_description',
              'value'       => $results->Item_description,
              'maxlength'   => '60',
              'size'        => '70',
              'style'       => 'width:70%',

<td>&lt;?php echo form_label('Item Description ','Item_description', $attributes); ?&gt;</td>
<td>&lt;?php echo form_input($Item_description); ?&gt;</td>
<td>&lt;?php echo form_error('Item_description'); ?&gt;</td>

// Controller
function edit($Item)
        $data['results'] = $this->product_model->getRecords(array('Item' => $Item));
        if (!$data['results'])
            $this->session->set_flashdata('flashError', 'Record not found, please contact your administrator.');
        // Fill Category Dropdown Box
        $data["cat"] = $this->category_model->fillCombo();
        // Validate form
        $this->form_validation->set_error_delimiters('<span style="color: red" class="error">', '</span>');        
        $this->form_validation->set_rules('Item_description', 'Item Description','trim|required');
        $this->form_validation->set_rules('Extended_description', 'Extended Description','trim|required|min_length[4]');
        $this->form_validation->set_rules('Size', 'Size','trim|required');
        $this->form_validation->set_rules('Sizenum', 'Sizenum','trim|required');
        $this->form_validation->set_rules('List_price', 'List Price','trim|required');
        $this->form_validation->set_rules('Price1', 'Price1','trim|required');
        $this->form_validation->set_rules('Price2', 'Price2','trim|required');
        $this->form_validation->set_rules('Price3', 'Price3','trim|required');
        $this->form_validation->set_rules('On_hand_qty', 'On Hand Qty','trim');
        if ($this->form_validation->run())
            //$_POST['Item'] = $Id;
            //if (empty($_POST['userPassword'])) unset($_POST['userPassword']);
            if ($this->product_model->updateRecord($_POST))
                $this->session->set_flashdata('flashConfirm', 'Record has been successfully updated.');
            else {
                $this->session->set_flashdata('flashError', 'Record not updated! No Changes were made.');
        $this->template->write('menu', anchor($this->session->userdata('returnPage'),'Cancel Request'));
        $this->template->write('title','Edit Record');
        $this->template->write_view('content','product/edit_form', $data, FALSE);            

Thank you...

Validation problem - El Forum - 03-21-2011

You're running validation without checking if the form has been submitted:

if ($this->form_validation->run())

// try

if($_POST && $this->form_validation->run())

Validation problem - El Forum - 03-21-2011

[eluser]Frank Rocco[/eluser]
Thanks, the error message is still showing.
Could this be a bug in the latest CI2?

Validation problem - El Forum - 03-22-2011

[eluser]Frank Rocco[/eluser]
It turns out, I was calling the edit form from my list page using a form post with an edit button. I changed it to an anchor and the problem was resolved.