I’m working on a CMS in Codeigniter and one main part is a form for creating and editing posts.
I’ve been planning on using the same view file for both since all of the elements are shared. The only difference would be the form is blank when creating and it’s populated when being edited. Is this the right way to go?
I was thinking about having a method for each, so post/create and post/edit($id).
In the create method in the post controller I have all the form data like this (for errors):
$this->data['item_title'] = array(
'name' => 'item_title',
'id' => 'item_title',
'type' => 'text',
'value' => $this->form_validation->set_value('item_title'),
);
I’m thinking about just altering the value to hold the database value instead of set_value(), so something like:
public function edit($id) {
$post_data = $this->post_model->get_post_data($id)
$this->data['item_title'] = array(
'name' => 'item_title',
'id' => 'item_title',
'type' => 'text',
'value' => $post_data['post_title'],
);
}
Am I on the right track or is there a better way to approach this? Should I just use 2 views?
i use a partial
_form.phpthat is shared by a new and edit controller action. on both actions i have the same validations so i moved those to the controller constructor, then for each input i just use a ternary operator that says if the existing value$titleis provided then populate the<input>value using it, otherwise use the codeigniterset_value()helper to populate with the validation value.