如何在一个视图中分隔两个验证表单?

时间:2012-10-02 17:09:52

标签: php forms codeigniter

我在CodeIgniter上的网站上的一个视图上有一个登录表单和一个创建成员表单。

问题在于,当我提交表单时出现错误(在LOGIN表单上按密码和电子邮件的空字段提交),它会触发创建成员表单中的电子邮件和密码字段,反之亦然。

我已经给了他们不同的提交按钮名称,仍然无法将它们分开。

我已尝试重命名字段,但是我对要重命名的值感到困惑,因为我必须向数据库提交特定值。

这是创建成员表单和控制器:

<?php

            echo form_open('auth/create_member');

            echo form_label('',  'email', array('type'=>'text'));
            $data = array( 'name' => 'first_name', 'class' => 'input', 'placeholder' => 'First Name' );
            echo form_input($data, set_value('first_name'));
            echo "<span class='errors'>";
            echo form_error('first_name');
            echo "</span>";

            echo form_label('',  'last_name', array('type'=>'text'));
            $data = array( 'name' => 'last_name', 'class' => 'input', 'placeholder' => 'Last Name' );
            echo form_input($data, set_value('last_name'));
            echo "<span class='errors'>";
            echo form_error('last_name');
            echo "</span>";

            echo form_label('',  'email', array('type'=>'text'));
            $data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email' );
            echo form_input($data, set_value('email'));
            echo "<span class='errors'>";
            echo form_error('email');
            echo "</span>";

            echo form_label('',  'password', array('type'=>'password'));
            $data = array( 'name' => 'password', 'class' => 'password', 'size' => 30, 'placeholder' => 'Password'  );
            echo form_password($data, set_value('sha1(password)'));
            echo "<span class='errors'>";
            echo form_error('password');
            echo "</span";

            echo form_label('',  'password2', array('type'=>'password'));
            $data = array( 'name' => 'password2', 'class' => 'input', 'size' => 30, 'placeholder' => 'Confirm Password'  );
            echo form_password($data, set_value('sha1(password2)'));
            echo "<span class='errors'>";
            echo form_error('password2');
            echo "</span>";

            echo form_submit('submit', 'Submit');
            echo form_close();
            ?>

function create_member() 
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required'); 
        $this->form_validation->set_rules('password2', 'Confirm Password', 'trim|required|matches[password]');

        if($this->form_validation->run() == FALSE)
        {
            $data['main_content'] = 'home/home_page';
            $this->load->view('includes/templates/home_page_template', $data);
        }
        else 
        {
            $this->load->model('user_model');
            if($query = $this->user_model->create_member())
            {
                $this->load->model('user_model');
                $this->varification_email();
                $data['main_content'] = 'account/welcome';
                $this->load->view('includes/templates/main_page_template', $data);

            }
            else
            {
                $this->load->view('home/home_page');
            }
        }
    }

这是登录表单和控制器:

<?php

                echo form_open('auth/validate_credentials_login');

                echo "<span class='errors_login'>";
                echo form_error('email');
                echo "</span>";

                echo form_label('',  'email',  array('type'=>'text'));
                $data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email');
                echo form_input($data, set_value('email'));


                echo "<span class='errors_login'>";
                echo form_error('password');
                echo "</span>";

                echo form_label('',  'password', array('type'=>'password'));
                $data = array( 'name' => 'password', 'class' => 'input', 'placeholder' => 'Password');
                echo form_password($data, set_value('sha1(password)'));

                echo form_submit('submit_login', 'Login');
                echo form_close();

                ?>      

function validate_credentials_login()
   {
        // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY

        $this->load->library('encrypt');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        $this->load->library('session');
        $this->load->model('user_model', 'um');
        $login = $this->input->post('submit_login');





   if($login) {
                $user = $this->um->validate_home_login(array('email' => $this->input->post('email')));
                if( $user ) {

                    // CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM
                    if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password')))) {
                        $this->session->set_userdata(array(
                            'email' => $this->input->post('email')
                        ));
                        redirect('account/dashboard');
                        exit;
                    }
                }
            }
   }

1 个答案:

答案 0 :(得分:2)

首先,您只需要if

中的所有验证规则

这意味着点击提交时..