CodeIgniter Forums
Having some trouble with form validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Having some trouble with form validation (/showthread.php?tid=13603)



Having some trouble with form validation - El Forum - 11-28-2008

[eluser]baalwww[/eluser]
Hi. I'm having some trouble with form validation. The docs show the validation example with a form that is displayed immediately. In my case, I'm returning the form in a variable, and then passing it as content to be displayed in the center section of a formatted page...

Code:
function login()
      {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('email','Email','required|valid_email|trim|xss_clean');
        $this->form_validation->set_rules('password','Password','required|trim|xss_clean');
        
        if ($this->form_validation->run() == FALSE)
        {
            $data['content']=$this->load->view('admin_login','',true);
        }
        else
        {
        }
            $this->load->view('frame',$data);

The else is empty because whether the login form should be shown or the login is successful, the formatted page is shown, so there's nothing needed in the else clause.

The problem is that the validation messages don't show. If the form doesnt validate, I just get the form again with no messages.


Having some trouble with form validation - El Forum - 11-29-2008

[eluser]iainco[/eluser]
Hey, can you show us your view code?

As a side, you can just skip the else {} altogether instead of having it empty.


Having some trouble with form validation - El Forum - 11-29-2008

[eluser]baalwww[/eluser]
Sure. Yeah, I left the empty else tags there to remind me later that I need to do some work with showing grayed icons in that instance instead of colored ones (the functionality isn't available to a user until logged in).

Here's the view code of the login form:

Code:
<form id="alogin" method="post" action="admin/process_login">
<label for="email">Email:</label>
&lt;input type="text" id="email" name="email" value="&lt;?php echo set_value('email');?&gt;" maxlength="50" size="50"&gt;
<label for="password">Password:</label>
&lt;input type="password" id="password" name="password" maxlength="50" size="50"&gt;
&lt;input type="submit" value="Log In"&gt;
&lt;/form&gt;

and here is the page it sits in

Code:
&lt;?php
$image[]=array('src'=>'images/settings.png','id'=>'settings');
$image[]=array('src'=>'images/color.png','id'=>'color');
$image[]=array('src'=>'images/layout.png','id'=>'layout');
$image[]=array('src'=>'images/navigation.png','id'=>'navigation');
$image[]=array('src'=>'images/mailing_list.png','id'=>'mailing_list');
$image[]=array('src'=>'images/newsletter.png','id'=>'newsletter');
$image[]=array('src'=>'images/send_newsletters.png','id'=>'send_newsletters');
$image[]=array('src'=>'images/cms.png','id'=>'cms');
?&gt;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Expires" content="Fri, Jan 01 1900 00:00:00 GMT"&gt;
&lt;meta http-equiv="Pragma" content="no-cache"&gt;
&lt;meta http-equiv="Cache-Control" content="no-cache"&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="content-language" content="en"&gt;
&lt;meta name="author" content=""&gt;
&lt;meta http-equiv="Reply-to" content="@.com"&gt;
&lt;meta name="generator" content="PhpED 5.2"&gt;
&lt;meta name="description" content=""&gt;
&lt;meta name="keywords" content=""&gt;
&lt;meta name="creation-date" content="09/20/2007"&gt;
&lt;meta name="revisit-after" content="15 days"&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url();?&gt;admin.css"&gt;
[removed]
&lt;!--
function imgSwitch(direction,img) {
    if (direction=='in')
        document.getElementById(img).src='&lt;?php echo base_url();?&gt;images/'+img+'_h.png';
    else document.getElementById(img).src='&lt;?php echo base_url();?&gt;images/'+img+'.png';
}
--&gt;
[removed]
&lt;/head&gt;
&lt;body&gt;
  <div id="framework">
    <div id="framework-top-bottom"></div>
    <div id="head">
        <div id="framework-left"></div>
        <div id="head-main"></div>
        <div id="framework-right"></div>
        <div id="clearboth"></div>
    </div>
    <div id="framework-top-bottom"></div>
    <div id="main-container">
        <div id="framework-left"></div>
        <div id="left-nav">
            &lt;?php $i=-1;?&gt;
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Settings</div>
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Colors</div>
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Select Layout</div>
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Navigation</div>
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Mailing List</div>
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Newsletters</div>
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Send Newsletters</div>
            <div id="left-nav-entry">&lt;?php echo img($image[++$i]);?&gt;Content</div>
        </div>
        <div id="framework-left"></div>
        <div id="content-main">&lt;?php echo $content;?&gt;<br /><br /></div>
        <div id="framework-right"></div>
        <div id="clearboth"></div>
    </div>
    <div id="framework-top-bottom"></div>
  </div>
&lt;/body&gt;
&lt;/html&gt;



Having some trouble with form validation - El Forum - 11-29-2008

[eluser]baalwww[/eluser]
Ah, found it :red:

The action in the form was calling a different function. I just needed to move all the validation information from the login() function to the process_login() function.