Welcome Guest, Not a member yet? Register   Sign In
form_validation on forms that handle add+edit
#11

[eluser]seanloving[/eluser]
I went through this add/edit dilemma too. The solution I came up with is this:

Code:
function add()
    {
        // create a new user
        
        // MODEL            
            $headerdata['title']="Add User";
            $maindata = $this->_form('add'); // returns blank form, or submitted form with errors
            $maindata['heading']="Add User";
            $maindata['button'] = anchor('contacts/users','go back');
            
        // VIEW
            $this->load->view('header', $headerdata);
            $this->load->view('index', $maindata);
            $this->load->view('footer');
    }    

    function edit($user_id='')
    {
        // display the form populated with data corresponding to the requested user
        // or else process an edit form submission
        
        // MODEL            
            if( $user_id )
            {            
                // form was requested
                // get the record corresponding to this $user_id (if one already exists)
                $_POST = $this->users_model->get_user_to_edit( $user_id );
                //print_r($_POST);exit;
            }
            // else data was posted
        
            // prepare remaining view information
            $headerdata['title']="Edit User";
            $maindata = $this->_form( $_POST );  // gets form data as an array
            $maindata['heading']="Edit User";
            $maindata['button'] = anchor('contacts/users','go back');
            
        // VIEW
            $this->load->view('header', $headerdata);
            $this->load->view('index', $maindata);
            $this->load->view('footer');
    }

Then I have a single function _form() which gets called by add(), edit(), and view()

Basically, you get to separate the validation logic that surrounds add and edit, which is slightly different for either case. Then in the _form() function I submit back to whoever called (add or edit).

Code:
function _form( $user )
    {
        // either "add a user" or "edit a user"
             $action = ($user=='add') ? 'add' : 'edit';
        // action is add or edit
            $this->form->open('contacts/users/'.$action)
...

-SL
#12

[eluser]boldyellow[/eluser]
I cobbled together a shared add/edit functionality in my controller and view.
It works fine for me, but maybe can be improved (since I'm still learning CI).

The key parts are:

View

Start by checking if an id is passed to the view (since we need an id in order to edit). If so, we're in edit mode, otherwise we're in add mode.

Code:
<? $ae = (isset($row->id)) ? 'edit' : 'add'; ?>

After form open, add a hidden field for the id, if we're in edit mode

Code:
<?=form_open('instructor_form'); ?>

<? if($ae == 'edit'): ?>
  <?=form_hidden('id', $row->id); ?>
<? endif; ?>

All form fields are theoretically identical for add and edit.
Code:
<fieldset>
    <label>Instructor Name</label>
    &lt;?=form_input('instructor_name', set_value('instructor_name')); ?&gt;
</fieldset>
...
Then at the form end, the submit button is named according to add or edit mode

Code:
<button type="submit" name="&lt;? echo ($ae == 'edit' ? 'editsubmit' : 'addsubmit'); ?&gt;">...


Controller

Set variable of db table name and check if posted submit button was to edit or to add. If no submit posted, then load the form filled in for updating or empty for inserting.

Code:
function instructor_add_edit()
{
    // db table name we'll need to insert or update
    $table = 'course_instructors';

    // check if submit posted was for edit or for add
    if ($this->input->post('editsubmit') OR $this->input->post('addsubmit'))
    {
        // load the validation (same for edit and add)        
        $this->_instructor_validation();
        
        // deal with validation errors

        // when validation passes will need posted id to update, but not to insert
        if ($this->input->post('editsubmit'))
        {
                $id = $this->input->post('id'); //
        }

        $data = array(
                'instructor_name' => $this->input->post('instructor_name'),
                'instructor_bio' => $this->input->post('instructor_bio')
        );

        // if edit mode, pass data to model's edit function
        if ($this->input->post('editsubmit'))
        {    
            $this->courses_model->edit_item($data, $id, $table);
        }
        // if add mode, pass data to model's add function
        elseif ($this->input->post('addsubmit'))  
        {
            $this->courses_model->add_item($data, $table);
        }
        else
        {
            // put in error handling if update or insert fails
        }
    }

    // if form not submitted, load the edit form or the add form

    // need an id to edit, so if an id is in the url, it's edit mode -- load filled in fields
    elseif(is_numeric($this->uri->segment(4)))  
    {
        $id = $this->uri->segment(4);
        $data['row'] = $this->courses_model->get_item($id, $table);
        $this->load->view('form_view', $data);
    }
    else // otherwise it's add mode so display empty fields
    {
        $this->load->view('form_view', $data);
    }
}
#13

[eluser]Paul Hernández[/eluser]
I liked the last post solution, I understood almost all, but I'm still having a doubt. If I want to open the form in edit mode, how can I populate the input fields with the values from the database. Only the hidden field is filled with the value of the corresponding id. What happen with the other input fields? I'm using ci2.

Regards
#14

[eluser]boldyellow[/eluser]
When you edit an item, presumably a list of entries displayed on a page, you click on an edit button that has the entry's id.

Item 1 --- EDIT (form/1)
Item 2 --- EDIT (form/2)
etc...

So when you click EDIT and go to the form to edit the entry, it grabs the id from the URL segment (this bit from the near end of the controller)...

Code:
// need an id to edit, so if an id is in the url, it's edit mode -- load filled in fields
    elseif(is_numeric($this->uri->segment(4)))  
    {
        $id = $this->uri->segment(4);
        $data['row'] = $this->courses_model->get_item($id, $table);
        $this->load->view('form_view', $data);
    }

Not on CI2 yet, but hope it'd be same idea...




Theme © iAndrew 2016 - Forum software by © MyBB