Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Seeking advice on trimming amount of code for a CRUD form
#1

[eluser]Nicholai[/eluser]
I'm starting to learn the ropes, but I feel like I'm making things harder than they need to be. I am creating a simple form that uses the same view for both adding and updating. When updating it populates the fields from the database, and when adding it presents blank fields. So far, I've found that I have to duplicate the form in order to make this work, like so:
Code:
<?php if ($query != ''): ?>
<?php foreach($query as $row):  ?>

<p>
&lt;?php echo form_open('tasktypes/insert'); ?&gt;
&lt;input type="hidden" value="&lt;?php echo $ID; ?&gt;" name="ID" /&gt;
<table>
<tr><td>Name: </td><td>&lt;input type="text" name="ttName" value="&lt;?php echo $row-&gt;ttName; ?&gt;"/></td></tr>
<tr><td>Description: </td><td>&lt;input type="text" name="ttDescription" value="&lt;?php echo $row-&gt;ttDescription; ?&gt;"/></td></tr>
<tr><td>Priority: </td><td>&lt;input type="text" name="ttPriority" value="&lt;?php echo $row-&gt;ttPriority; ?&gt;"/></td></tr>
<tr><td>Display Order: </td><td>&lt;input type="text" name="ttDisplayOrder" value="&lt;?php echo $row-&gt;ttDisplayOrder; ?&gt;"/></td></tr>
<tr><td>&lt;input type="image" src="http://nicholaibu-d630:8080/CodeIgniter/images/edit.png" value="Add" /&gt;&lt;/td></tr>
&lt;/form&gt;
</p>
&lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;


&lt;?php if (!$ID): ?&gt;
&lt;?php echo form_open('tasktypes/insert'); ?&gt;
<table>
<tr><td>Name: </td><td>&lt;input type="text" name="ttName" value=""/&gt;&lt;/td></tr>
<tr><td>Description: </td><td>&lt;input type="text" name="ttDescription" value=""/&gt;&lt;/td></tr>
<tr><td>Priority: </td><td>&lt;input type="text" name="ttPriority" value=""/&gt;&lt;/td></tr>
<tr><td>Display Order: </td><td>&lt;input type="text" name="ttDisplayOrder" value=""/&gt;&lt;/td></tr>
<tr><td>&lt;input type="image" src="http://nicholaibu-d630:8080/CodeIgniter/images/edit.png" value="Add" /&gt;&lt;/td></tr>
&lt;/form&gt;
</p>
&lt;?php endif; ?&gt;

My model checks if an ID is being passed in; if yes, it updates the record and if no, it adds a new record. My controller is pretty simple:

Code:
function add_update()
    {
        $data['title'] = "Task Types";
        
        $tasktypeID = $this->uri->segment(3);
        $data['ID'] = $tasktypeID;

        $data['query'] = $this->Admin_tasks_model->get_task_types($tasktypeID);

        
        $this->load->view('admin_menu_view.php', $data);
        $this->load->view('admin_add_new_task_type_view.php', $data);
    }

    function insert()
    {
    
        $this->Admin_tasks_model->ID = ($_POST['ID']);
        $this->Admin_tasks_model->save($_POST);
        redirect('tasktypes/display');
    }

Is there an easier way to reuse the form? It seems like the way I've set it up, form validation won't play nicely either. Should I split add and edit into two views (still using the same controller and model?)

I've been doing lots of reading and testing (and failing), trying to understand the "right way" to do things. Thanks for any advice.
#2

[eluser]Colin Williams[/eluser]
All you need to do is sort out the form values in the controller, then pass those values on to the view. Take a step back, sketch it out on notebook paper.

Also, skimming through the screencasts at http://ellislab.com/forums/viewthread/92009/ might reveal some ideas for your own process. The series uses the old validation class and never gets to the editing part, but you can extrapolate how to do it, I'd imagine.

Just think of the view as stupid. It just prints out values into controls and elsewhere. Let the controller sort out the details.
#3

[eluser]Nicholai[/eluser]
Thanks again, you've been so great! Your screencasts helped a lot in understanding best practices and keeping the code clean and readable. I'm going to watch them two or three more times and then try implementing my CRUD functions similarly. Is the sample code available for download anywhere by chance?
#4

[eluser]Colin Williams[/eluser]
Let me get it out there somewhere. One moment...
#5

[eluser]Colin Williams[/eluser]
Here, you go. The example controller does have an edit function, so you can see how that works.
#6

[eluser]Nicholai[/eluser]
Thanks Colin! Your screencast and sample code have helped out immensely; the maintainers of the User Guide could learn a few things from you, since most sites are data-driven but the user guide examples are not. I was able to extrapolate how to use the new form validation class. The first time I hit the form to add a record, I received 'undefined index' for each of my fields. After entering some data and breaking a rule, the POST array populated so the errors went away. I moved some logic around and I think I've resolved the error the 'right' way.

Here is what I've got, and it seems to be working great. Posting my controller code here for any noobs like me who come across this thread in the future:

Code:
function _tasktypes_form()
    {
        $form = array(
                   array(
                         'field'   => 'ttName',
                         'label'   => 'Name',
                         'rules'   => 'required'
                      ),
                   array(
                         'field'   => 'ttDescription',
                         'label'   => 'Description',
                         'rules'   => 'required'
                      ),
                   array(
                         'field'   => 'ttPriority',
                         'label'   => 'Priority',
                         'rules'   => 'numeric'
                      ),  
                   array(
                         'field'   => 'ttDisplayOrder',
                         'label'   => 'Display Order',
                         'rules'   => 'numeric'
                      ),
        );
                
        return $form;
    }
    

    function _tasktypes_form_defaults()
    {
            $values = array(
                         'ID'   => '',
                         'ttName'   => '',
                         'ttDescription'   => '',
                         'ttPriority'   => '',
                         'ttDisplayOrder'   => '',
                      );
        return $values;
    }
    
    
    function add()
    {
        $form = $this->_tasktypes_form();
        $this->form_validation->set_rules($form);    

        $form['title'] = 'Add a Task Type';
        $form['action'] = site_url('tasktypes/add');
        $form['button_text'] = 'Add Task Type';
        
        if ($this->form_validation->run() == FALSE)
        {
            if (! $_POST)
            {
            $form['values'] = $this->_tasktypes_form_defaults();
            }
            else
            {
            $form['values'] = $_POST;
            }
        
        $this->load->view('admin_add_new_task_type_view', $form);
        }
        else
        {
            if ($this->Admin_tasks_model->save($_POST))
            {
                redirect('tasktypes/display');
            }
            else
            {
                $form['error'] = "Don't know why, but it failed";
                $form['values'] = $_POST;
                $this->load->view('admin_add_new_task_type_view', $form);
            }
  
        }
#7

[eluser]Colin Williams[/eluser]
Nice work updating it for 1.7's Form validation class. Smile




Theme © iAndrew 2016 - Forum software by © MyBB