CodeIgniter Forums
Form Validation Default values - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form Validation Default values (/showthread.php?tid=42262)



Form Validation Default values - El Forum - 06-01-2011

[eluser]cideveloper[/eluser]
I will post my code below but here is the issue first. I am using form validation with an update form. So on a page that lists items, I can click a link to go to an update form that will let me update that specific item. The controller checks if FV is true. If not it will grab the info for that item from the db, show the form, and populate the fields. When submitted again the controller checks if FV is true. If not I get this error.

Code:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/update_form.php
Line Number: 8

Actually I get an error for all the prepopulated fields. I understand why this is happening. The post url for the form is going to the same controller but without an id field to search the db thus returning nothing. But at this point I dont even want to search the db since the form is already populated and submitted. Am I coding this wrong? Is there a good way around this? I was thinking maybe check if the form is posted and if not do not do the DB search but then the default values in the view are still expecting something. Sorry for the extended version but just trying to give as much info as possible.

Controller
Code:
public function update_item($item_id = null) {
        if ($this->form_validation->run() == FALSE)
        {
            $data = array (
                'message' => (validation_errors()) ? validation_errors() : $this->session->flashdata('message'),
                'info' => $this->todo_model->get_record($item_id)
            );

            $this->load->vars($data);
            $this->load->view('header');
            $this->load->view('update_form');
            $this->load->view('footer');
        }
        else
        {
            $id = $this->input->post('id', TRUE);
            $data = array (
                'title'  => $this->input->post('title', TRUE),
                'notes'  => $this->input->post('notes', TRUE),
                'start_date'  => $this->input->post('start_date', TRUE),
                'end_date'  => $this->input->post('end_date', TRUE)
            );
            if($this->todo_model->update_item($data, $id)) {
                $this->session->set_flashdata('message', '<span class="good">Updated Item ' . $id . '</span>');
            }
            redirect('/todo');
        }            
    }
View
Code:
<div id="notifications">
                &lt;?=$message;?&gt;
            </div>
            <div id="todo_form">
                &lt;?=form_open('todo/update_item');?&gt;
                <p>
                    &lt;?=form_label('Title:', 'title');?&gt;
                    &lt;?=form_input('title', set_value('title', $info->title));?&gt;
                </p>
                <p>
                    &lt;?=form_label('Notes:', 'notes');?&gt;
                    &lt;?=form_textarea('notes', set_value('notes', $info->notes))?&gt;
                </p>
                <p>
                    &lt;?=form_label('Start Date:', 'start_date');?&gt;
                    &lt;?php
                    $data = array (
                        'name' => 'start_date',
                        'id' => 'start_date',
                        'class' => 'date_picker',
                        'value' => set_value('start_date', $info->start_date)
                    );
                    echo form_input($data);
                    ?&gt;
                </p>
                <p>
                    &lt;?=form_label('End Date:', 'end_date');?&gt;
                    &lt;?php
                    $data = array (
                        'name' => 'end_date',
                        'id' => 'end_date',
                        'class' => 'date_picker',
                        'value' => set_value('end_date', $info->end_date)
                    );
                    echo form_input($data);
                    ?&gt;
                </p>
                <p>
                &lt;?=form_hidden('id', $info->id);?&gt;
                &lt;?=form_submit('mysubmit', 'Submit Item!');?&gt;
                </p>
                &lt;?=form_close('');?&gt;
            </div>
Model
Code:
public function update_item($data = array(), $id) {
        $this->db->where('id', $id);
        $this->db->update('todo', $data);    
        if($this->db->affected_rows()>0) {
            return TRUE;
        }
        return FALSE;
    }