codeIgniter form validation doesn't work when passing view as string

431 Views Asked by At

I am currently learning CodeIgniter and using it for a small project. I would like to make a template so that I don't need to write repeated code for the views. I like jruzafa's answer in this post: How to Deal With Codeigniter Templates?

In controller:

    //Charge the view inside array
    $data['body'] = $this->load->view('pages/contact', '', true);


    //charge the view "contact" in the other view template
    $this->load->view('template', $data);

In view template.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es"> 
<head> 
    <title>Template codeigniter</title> 
</head> 
<body> 
    <div> 
        <?=$body?>
    </div> 
    <div class="clear"></div> 
    <div>Footer</div> 
    </div> 
</body> 
</html> 

$body is the view contact.

But now I face a problem. The form validation doesn't work if I pass the form_view as string to $data['body']. Is there any way to work around it?

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Try loading the body content within the template itself. This should allow you to be more selective with your output:

In controller:

// Set the path to body content inside the $data array
$data['path_to_body'] = 'pages/contact';
$this->load->view('template', $data);

In view template.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es"> 
<head> 
    <title>Template codeigniter</title> 
</head> 
<body> 
    <div> 
        <? $this->load->view($path_to_body) ?>
    </div> 
    <div class="clear"></div> 
    <div>Footer</div> 
    </div> 
</body> 
</html>