Welcome Guest, Not a member yet? Register   Sign In
Dealing with tabbed interface and validation of multiple forms
#1

[eluser]Unknown[/eluser]
Hi everyone,

I'm working with CI on a project which implies a tabbed interface.

The contents of all the tabs will be rendered in the same view file which will be handled by a controller so that there will be no page load when the user will click on the tabs. The tab system will be done in Javascript.

Now I got a problem, let me show you my code.
The view file back_configuration_view.php looks like this :

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;title&gt;My Website - Item 1 page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    <h1>Menu Items</h1>
    <ul>
        <li>&lt;?=anchor('back_page1', 'Item 1');?&gt;</li>
        <li>&lt;?=anchor('back_page2', 'Item 2');?&gt;</li>
    </ul>
    
    &lt;!--Tab 1--&gt;
    <div>
        <h2>Tab 1</h2>
        &lt;?=$feedback_tab4?&gt;
        &lt;form name="conditions" action="&lt;?=base_url().'back_configuration/index/cgv'?&gt;" method="post"&gt;
            &lt;?=$this->validation->cgv_error; ?&gt;
            <p>&lt;textarea name="cgv"&gt;&lt;?=$cgv?&gt;&lt;/textarea&gt;</p>
            <p>&lt;input type="submit" value="Save" /&gt;</p>
        &lt;/form&gt;
    </div>
    
    &lt;!--Tab 2--&gt;
    <div>
        <h2>Tab 2</h2>
        &lt;form name="form_tab2" action="&lt;?=base_url().'back_configuration/index/adresses_email'?&gt;" method="post"&gt;
            &lt;?=$this->validation->email_admin_error; ?&gt;
            <p><label>Admin email address</label>&lt;input type="text" name="email_admin" value="&lt;?=$email_admin?&gt;" /&gt;</p>
            &lt;?=$this->validation->email_notifications_error; ?&gt;
            <p><label>Contact email address</label>&lt;input type="text" name="email_contact" value="&lt;?=$email_contact?&gt;" /&gt;</p>
            <p>&lt;input type="submit" value="Save" /&gt;</p>
        &lt;/form&gt;
    </div>
&lt;/body&gt;
&lt;/html&gt;

As you can see, I got 2 tabs on the same page, each tab includes a form.
Now lets see the controller back_configuration.php :

Code:
&lt;?php
class Back_configuration extends Controller {
    function Back_configuration() {
        parent :: Controller();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('validation', '', TRUE);//Loads Validation Library
        $this->load->model('Dbbackset', '', TRUE);
        $this->load->model('Dbbackget', '', TRUE);
    }
    function index() {
        $data['feedback_tab1']= $this->_adresses_email();
        $data['feedback_tab2']= $this->_cgv();
        //Get the datas to edit
        $emails = $this->Dbbackget->getEmails();
        $data['email_admin'] = $emails['email_admin'];
        $data['email_contact'] =$emails['email_contact'];
        $data['cgv'] = $this->Dbbackget->getCGV();
        $this->load->view('back_configuration_view', $data);
    }
    function _adresses_email() {
        $renvoi= "";
        $this->validation->email_admin_error= "";
        $this->validation->email_contact_error= "";
        $rules['email_admin']= "required|valid_email";
        $rules['email_contact']= "required|valid_email";
        $this->validation->set_rules($rules);
        //Repopulating the form
        $fields['email_admin']= 'email_admin';
        $fields['email_contact']= 'email_contact';
        $this->validation->set_fields($fields);
        if ($this->validation->run() == TRUE) {
            $tab_emails= array (
                'email_admin' => $_POST['email_admin'],
                'email_contact' => $_POST['email_contact']
            );
            $update= $this->Dbbackset->edit_email($tab_emails);
            if ($update == 1) {
                $renvoi= "Update done.";
            }
            else {
                $renvoi= "Update error.";
            }
        }
function _cgv() {
        $renvoi= "";
        $this->validation->cgv_error= "";
        $rules['cgv']= "required";
        $this->validation->set_rules($rules);
        //Repopulating the form
        $fields['cgv']= 'cgv';
        $this->validation->set_fields($fields);
        if ($this->validation->run() == TRUE) {
            $update= $this->Dbbackset->edit_cgv($_POST['cgv']);
            if ($update == 1) {
                $renvoi= "Update done.";
            }
            else {
                $renvoi= "Update error.";
            }
        }
        return $renvoi;
    }
?&gt;

As you can see, the only public function of my controller is index(). index() loads the two private functions _adresses_email() and _cgv(). Each of these two functions should handle one of the two form "displayed" on my view. The problem I got is that Validation works fine for the first form but silently fails.

I understand why this is happening : Validation is loaded for the entire controller and in fact I use the same instance of the Validation library to handle both forms. So when I try to validate the second form, required fields of the first form are missing, preventing the validation to work properly.

Here's the question : is there a mean to instanciate more than one validation library in my case?

I've tried to do something like that :
Code:
$tab1= & get_instance();
$tab1->load->library('validation', '', TRUE)
And then calling $tab1->validation ... instead of $this->validation. It doesn't work at all.

Maybe what I want to do is impossible the way I want it to be done Smile In that case, what is the cleanest way to make that stuff working fine?

Any help or piece of advice will be really appreciated. Thanks in advance.
#2

[eluser]Unknown[/eluser]
Ok, I finally managed to solve the problem using the advice of Jalol http://codeigniter.com/wiki/Validation_a...ple_forms/

Maybe it's not as clean as I would like it to be, but at least it works Smile

If anyone has a suggestion to make my code cleaner, please do not be shy Smile




Theme © iAndrew 2016 - Forum software by © MyBB