Welcome Guest, Not a member yet? Register   Sign In
Not understanding form validation
#1

[eluser]cominus[/eluser]
After installing form validation, I can still click the submit button on the form and continue to the next page successfully - no error messages, etc.

Here is the controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
    }

    function index()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('member_email','Your Email','required');
        $this->form_validation->set_rules('password','Password','required');
        
        $data['page_title'] = 'Login';
        $data['page_heading'] = 'Login to the Bomra';

        $this->load->view('header', $data);
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('loginview', $data);
        }
        
        $this->load->view('footer');
    }

Here is the form view:
Code:
<div id="form_login">

    &lt;?php echo validation_errors();?&gt;
    
    &lt;?php echo form_open('login/loggedin'); ?&gt;
    &lt;?php echo form_fieldset('Login Form'); ?&gt;<br /><br />
    &lt;?php echo form_label('Member Email','member_email'); ?&gt;
        &lt;?php $email_data = array(
                        'name' => 'member_email',
                        'id' => 'member_email',
                        'value' => '',
                        'maxlength' => '60',
                        'size' => '30'
                        );
        echo form_input($email_data);
        ?&gt;<br /><br />
    &lt;?php echo form_label('Password','password'); ?&gt;
        &lt;?php $password_data = array(
                        'name' => 'password',
                        'id' => 'password',
                        'value' => '',
                        'maxlength' => '60',
                        'size' => '30'
                        );
        echo form_password($password_data);
        ?&gt;<center><br /><br />
    &lt;?php echo form_submit('loginsubmit','Submit Login'); ?&gt;
    <br /><br /></center>
    &lt;?php echo form_fieldset_close(); ?&gt;
    &lt;?php echo form_close(); ?&gt;

</div>

Here is the login page view:
Code:
<div id="content-1col">

    &lt;?php $this->load->view('form_login'); ?&gt;

</div>

There must be something missing, but I cannot see it. Thanks for looking at this.
#2

[eluser]JuanitoDelCielo[/eluser]
You should send it to to same controller and method over an over in order to get the errors

Code:
class User extends CI_Controller {

    public function insert() {

        $this->form_validation->set_rules('email', 'E-mail', 'required|trim');

        if ( $this->form_validation->run() ) {

            //Insert into the database
            //Succesful message

        } else {

            $this->load->view('newuserform');

        }

    }

}

View

Code:
&lt;?php echo form_open('user/insert'); ?&gt;
    &lt;?php echo form_label('E-mail', 'email'); ?&gt;
    &lt;?php
        $input = array(
        'name'  => 'email',
        'class' => 'txt',
        'value' => set_value('email')
    );
        echo form_input($input);
    ?&gt;
    &lt;?php echo form_submit('', 'Create User',); ?&gt;
&lt;?php echo form_close(); ?&gt;
#3

[eluser]cominus[/eluser]
Thanks, JuanitoDelCielo, but it looks like you are trying to show me a better way to do the form - or forms, in your view. As I look at it, your example does not accomplish the same thing - and it gives me no clue why the validation code I submitted does not work. The code I presented is almost exactly as it is written in the manual for a basic login with validation. I need to know why the validation does not work. Any help in this regard will be much appreciated. Thanks.
#4

[eluser]InsiteFX[/eluser]
becuase you do not have the else statement!

InsiteFX
#5

[eluser]cominus[/eluser]
Thanks for the tip, InsightFX, but that did not cure the problem. The reason for lack of else statement was because the form implied the else with
Code:
&lt;?php echo form_open('login/loggedin'); ?&gt;
which corresponded with the loggedin() function of the login controller (which was not displayed in my previous code segments. Nonetheless, I have updated the controller with
Code:
if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('loginview', $data);
        } else {            
            $this->load->view('loggedinview', $data);
        }
but even with this, the viewer can click the empty form's submit button and travel to the next page without validation.

Any other ideas? I have spent days on this. All googling points me to the same examples and instructions I am using. Do I have a defective codeigniter framework?
#6

[eluser]LuckyFella73[/eluser]
JuanitoDelCielo posted the right way for you - the most important information
he posted for you is the form action attribute.

If you set up your validation rules in controller_A / method_a
you form has to be send to controller_A / method_a.

You did set up the rules in Login / index but your form is send to
login / loggedin. You didn't post the method "loggedin" but it seems
that your validation rules are placed like you posted.

Did you try to adjust the action attribute? Just give it a try!
#7

[eluser]cominus[/eluser]
Lucky, I am not sure I am understanding what you are saying. So I will post the entire controller. Do you mind explaining it to me in that context? and if you need another file, let me know.
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
    }

    function index()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('member_email','Your Email','trim|required|valid_email');
        $this->form_validation->set_rules('password','Password','trim|required|md5');
        
        $data['page_title'] = 'Login';
        $data['page_heading'] = 'Login to the Bomra';

        $this->load->view('header', $data);
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('loginview', $data);
        } else {            
            $this->load->view('loggedinview', $data);
        }
        
        $this->load->view('footer');
    }
    
    function loggedin()
    {
        $data['page_title'] = 'Loggedin';
        $data['page_heading'] = 'Hey! Hey! You have logged in to the Bomra';
        $this->load->view('header', $data);
        $this->load->view('loggedinview', $data);
        $this->load->view('sidebar');
        $this->load->view('footer');
    }        
    
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */
#8

[eluser]cominus[/eluser]
[quote author="LuckyFella73" date="1302212670"]JuanitoDelCielo posted the right way for you - the most important information
he posted for you is the form action attribute.
[/quote]

LuckyFella73 - you are right (and JuanitoDelCielo). At first, I did not see the relationship to my project. But after looking at the code a little closer, I see what Juanito was getting at. Tried it and it works. The form initiates and processes through the same controller/method. I see that now. Thanks to both of you!! Thanks a lot - that ends two days of recursive drudge and now I can get back to work!!!
#9

[eluser]cominus[/eluser]
Just in case anyone else needs this - here is what the controller now looks like:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
    }

    function index()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('member_email','Your Email','trim|required|valid_email');
        $this->form_validation->set_rules('password','Password','trim|required|md5');

        if ($this->form_validation->run() == FALSE)
        {
            
            $data['page_title'] = 'Login';
            $data['page_heading'] = 'Login to the Bomra';
            $this->load->view('header', $data);            
            $this->load->view('loginview', $data);
            
        } else {            
            
            $data['page_title'] = 'Loggedin';
            $data['page_heading'] = 'You Are Logged-In to the Bomra';
            $this->load->view('header', $data);            
            $this->load->view('loggedinview', $data);
            $this->load->view('sidebar');
            
        }
        
        $this->load->view('footer');
    }
    
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */

And the form looks like:
Code:
<div id="form_login">

    &lt;?php echo validation_errors();?&gt;
    
    &lt;?php echo form_open('login/'); ?&gt;
    &lt;?php echo form_fieldset('Login Form'); ?&gt;<br /><br />
    &lt;?php echo form_label('Member Email','member_email'); ?&gt;
        &lt;?php $email_data = array(
                        'name' => 'member_email',
                        'id' => 'member_email',
                        'value' => set_value('member_email'),
                        'maxlength' => '60',
                        'size' => '30'
                        );
        echo form_input($email_data);
        ?&gt;<br /><br />
    &lt;?php echo form_label('Password','password'); ?&gt;
        &lt;?php $password_data = array(
                        'name' => 'password',
                        'id' => 'password',
                        'value' => '',
                        'maxlength' => '60',
                        'size' => '30'
                        );
        echo form_password($password_data);
        ?&gt;<center><br /><br />
    &lt;?php echo form_submit('loginsubmit','Submit Login'); ?&gt;
    <br /><br /></center>
    &lt;?php echo form_fieldset_close(); ?&gt;
    &lt;?php echo form_close(); ?&gt;

</div>
#10

[eluser]LuckyFella73[/eluser]
Glad to hear you got your script to work!




Theme © iAndrew 2016 - Forum software by © MyBB