EllisLab text mark
Advanced Search
     
Dealing with tabbed interface and validation of multiple forms
Posted: 29 June 2007 05:17 AM
Joined: 2007-06-25
3 posts

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 :

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

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 :

<?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;
    
}
?> 

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 :

$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.

 
Posted: 29 June 2007 06:32 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-06-25
3 posts

Ok, I finally managed to solve the problem using the advice of Jalol http://codeigniter.com/wiki/Validation_and_multiple_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