CodeIgniter Forums

Full Version: Validation problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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?

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

<tr>
<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>
</tr>

// 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.');
            redirect('product');
        }
        
        // 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.');
                redirect('product');
            }
            else {
                $this->session->set_flashdata('flashError', 'Record not updated! No Changes were made.');
                redirect('product');
            }
        }
        $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);            
        $this->template->render();
    }

Thank you...

El Forum

[eluser]danmontgomery[/eluser]
You're running validation without checking if the form has been submitted:

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

// try

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

El Forum

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

El Forum

[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.