Welcome Guest, Not a member yet? Register   Sign In
Using a different controller to perform validation than the one creating the form
#1

[eluser]johnwbaxter[/eluser]
I've got a form that is created by a controller. The form submits to a different controller that does some stuff if the form content is valid.

My problem is that if the validation fails, i want to go back to the form creating controller and populate the view with the error message. redirect() does not work for this.

Has anyone got any ideas on how i could do this?

Thanks!
#2

[eluser]gnomishperson[/eluser]
I have this same problem...

I have a controller that loads the data into variables and sends that data onto the view file.

On the view file that contains all the loaded data also contains a form that POSTs to a different controller (or method [function]) that does the validating to determine whether or not the $_POST should be stored in the DB.

The problem is reloading the selected record from the first controller with validate errors if the validation fails, but unfortunately my trials with a redirect lead to me the conclusion the $_POST or any other data is not kept with a re-direct, and routes haven't seemed to be working for me.

This is basically my code (short-hand version):
Controller:
Code:
<?php
class Pathologist extends Controller
{    
    function Pathologist()
    {
        parent::Controller();

        // VALIDATION SETTINGS
        $this->load->library('validation');
        
        $fields['pathologic_description']     = 'Final Pathologic Description';
        $fields['microscopic_description']    = 'Microscopic Description';
        
        $this->validation->set_fields($fields);
    }
    
    /**
     * Displays home page of Admin Console
     *
     */
    function index()
    {      
        //listing records as links to details of record based on logged in user.
    }
   /**
     * View record details
     *
     * @param record id $id
     */
    function show($id = '')
    {                    
        ($id == '' ? $id = $this->uri->segment(4) : $id);
        $query = $this->getCaseById($id);
        
        if ($query->num_rows() == 1)
        {
            $row = $query->row();
                
            $data['pathologist']['id'] = $row->id;
                        /* Rest of fields being set
                        ** $data['pathologist']['etc']
                        */
            
                        //Comments                        
            $comments = $this->getComments($row->id);
            //rest of comment code would be here

        //Function called from form within view file.
    function findings()
    {
        
        $id      = $this->input->post('id');
        $case_id = $this->input->post('case_id');
        $final      = $this->input->post('pathologic_description');
        $micro      = $this->input->post('microscopic_description');
        $save      = $this->input->post('save');
        $submit  = $this->input->post('submit');
        
        $rules['pathologic_description']  = "trim|required|xss_clean";
        $rules['microscopic_description'] = "trim|xss_clean";
        
        $this->validation->set_rules($rules);

        if ($this->validation->run() == FALSE)
        {            
            $this->show($id);
        }
        else
        {
            if (!$submit)
            {
                echo 'Attempting to Save';
                //$this->saveCase($id, $final, $micro);
            }
            else
            {
                echo 'Trying to submit';
                //$this->submitCase($id, $final, $micro);
            }
        }
View File:
Code:
<?php $rows = 5; $cols = 88; ?>
<h2>&lt;?=$action?&gt;</h2>

<p class="error">&lt;?=$this->validation->error_string?&gt;</p>

&lt;!--USERPROFILE DATA--&gt;

&lt;!-- START DETAILS DATA--&gt;
<div class="details">
<fieldset>
    <legend>Case #: &lt;?=$pathologist['case_id']?&gt;</legend>
    <ul>
        <li>List of Items to Display</li>
    </ul>
</fieldset>
<fieldset>
    <legend>Pathologist Findings</legend>
        //This is the form in question
    &lt;?=form_open('pathologist/'.$controller.'/findings')?&gt;
    <p>
        Final Pathologic Description<br />
        &lt;?=form_textarea(array('name' => 'pathologic_description',
                               'id'      => 'pathologic_description',
                               'value'=> (isset($this->validation->pathologic_description)) ? ($this->validation->pathologic_description) : ($pathologist['pathologic_description']),
                               'rows' => $rows,
                               'cols' => $cols
                               ))?&gt;
    </p>
    <p>
        Microscopic Description<br />
        &lt;?=form_textarea(array('name' => 'microscopic_description',
                               'id'      => 'microscopic_description',
                               'value'=> (isset($this->validation->microscopic_description)) ? ($this->validation->microscopic_description) : ($pathologist['microscopic_description']),
                               'rows' => $rows,
                               'cols' => $cols
                               ))?&gt;
    </p>
    &lt;?=form_hidden('id', $pathologist['id'])?&gt;
    &lt;?=form_hidden('case_id', $pathologist['case_id'])?&gt;
    &lt;?=form_submit(array('class' => 'submit',
                         'name'  => 'save',
                         'id'     => 'save',
                         'value' => 'Save'
                         ))?&gt;
    &lt;?=form_submit(array('class' => 'submit',
                         'name'  => 'submit',
                         'id'    => 'submit',
                         'value' => 'Submit Findings'
                         ))?&gt;
    &lt;?=form_close()?&gt;
</fieldset>
<fieldset>
    <legend>Comments</legend>
    //Comments section
</fieldset>
&lt;?=form_open('pathologist/'.$controller)?&gt;
&lt;?=form_submit(array('class'=>'submit',
                     'name'=>'back',
                     'id'=>'submit',
                     'value'=>'Back'
                     ))?&gt;

&lt;?=form_close()?&gt;
</div>

Basically my URI ends up being http://base.url/pathologist/pathologist/findings when I need it to be http://pathologist/pathologist/show/$case_id/ if the validation fails.
Still very much a rough draft, but my problem is still the same as the one mentioned above.
#3

[eluser]gnomishperson[/eluser]
I believe I solved my problem by putting the validation into the show() function and using findings() as a process function only, and using the form to submit to /show/$case_id.

Change to the end of show():
Code:
show()
{
            /*********************
            ** Added Validation **
            *********************/

            $rules['pathologic_description']  = "trim|required|xss_clean";
            $rules['microscopic_description'] = "trim|xss_clean";
            
            $this->validation->set_message('required', 'You are missing one or more required fields');        

            $this->validation->set_rules($rules);

            if ($this->validation->run() == FALSE)
            {
                $this->load->vars($data);

                $this->load->view($this->_container);
            }
            else
            {
                $this->findings();
            }
Change to findings():
Code:
function findings()
    {
      
        /***********************
        ** Removed Validation **
        ***********************/
            if (!$submit)
            {
                echo 'Attempting to Save';
                //$this->saveCase($id, $final, $micro);
            }
            else
            {
                echo 'Trying to submit';
                //$this->submitCase($id, $final, $micro);
            }
    }
Change to detail view:
Code:
&lt;?=form_open('pathologist/'.$controller.'/show/'.$pathologist['case_id'])?&gt;

I feel kinda silly about all this now, after the fix seemed so simple and obvious.




Theme © iAndrew 2016 - Forum software by © MyBB