【问题标题】:CodeIgniter form validation helper always falseCodeIgniter 表单验证助手总是假的
【发布时间】:2014-08-22 21:51:09
【问题描述】:

我正在使用 Codeigniter,除此之外我还有 Bonefire(这可能是问题吗?),问题是每次我想使用 Codeigniters 助手验证表单时,我的条件运行的第一个条件(FALSE)和在该功能之上 validation_errors() 没有运行......就像我的这个助手的库甚至没有加载,尽管按照书本做了所有事情:

if ($this->form_validation->run() == FALSE)
{          
    echo $msg = validation_errors();
}
else
{       
    $this->load->user_model->insert($data);
    echo $msg = "Registration successfull";
}

让我先发布我的表单(我按目的省略了内联样式和类):

<div class="" style="">
    <h1 id="header" class="">Login/Register</h1>
    <form action="/public/index.php/users/sportappregister" >
        <div style=""><input id="email" type="text" name="email" value="email"  style=""></div>
        <div style=""><input id="pass" type="text" name="password" value="password"  style=""></div>
        <div style="" class=""><img class="" style="" src="<?php echo img_path(); ?>ikone/fb_login_icon.png" />Login with Facebook</div>
        <div id="send" style="" class=""><input type="submit"> Submit </div>
        <div id="cancel" style="" class=""> Cancel </div>
    </form>
</div>

正如您可以从表单操作中读取的那样,我的控制器像往常一样位于公共类“sportappregister”下的文件“users”中class Users extends Front_Controller,最后在这个类中我创建了自己的函数来处理表单,如下所示:

public function sportappregister(){

    $email= ($this->input->get("email"));
    $pass = ($this->input->get("password"));

    $data = array(
              "email" => $email,
              "password" => $pass );

    // here I load my helper            
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    // rules for my form
    $this->form_validation->set_rules('email', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run() == FALSE)
    {
        echo $msg = validation_errors();
    }
    else
    {
        $this->load->user_model->insert($data);
        echo $msg = "Registration successfull";
    }
}

【问题讨论】:

    标签: php codeigniter helper


    【解决方案1】:

    您正在使用 `GET` 方法。 codeigniter 表单验证仅适用于 `POST` 方法。

    使用form_open()form_close()等CI表单标签来构建表单。 你可以查看This link

    使用 get 登录表单会使您的应用不安全。

    您的其余代码对我来说似乎没问题。 改变这个

    $email= ($this->input->post("email")); //changed get to post in both
    $pass = ($this->input->post("password"));
    

    【讨论】:

    • 使用 post 而不是 get 并且控制器没有收到我的输入值!是否有必要使用表单助手,因为我真的不习惯使用它?
    • @LazyPeon 然后在此处添加&lt;form action="/public/index.php/users/sportappregister" method="POST"&gt;。如果启用了 CSRF,您将遇到麻烦,因为您是手动制作表单而不是插入隐藏的 CSRF 字段。
    • 是的,我有问题,现在我收到 php 错误:“不允许您请求的操作。”什么是 CSRF?
    • @LazyPeon 这就是我所说的 CSRF。将此添加到您的表单中&lt;input type="hidden" name="&lt;?php echo $this-&gt;security-&gt;csrf_token_name; ?&gt;" value="&lt;?php echo $this-&gt;security-&gt;csrf_hash; ?&gt;" /&gt;
    • 我得到“未定义的属性:MY_Security::$csrf_hash”,你介意解释一下我们在这里做什么吗?
    【解决方案2】:

    有几件事我会改变。阅读下面修改后的函数中的cmets;

          public function sportappregister()
          {
                    // Load these first
                    $this->load->helper(array('form', 'url'));
                    $this->load->library('form_validation');
    
                    // Now set the rules
                    $this->form_validation->set_rules('email', 'Username', 'required');
                    $this->form_validation->set_rules('password', 'Password', 'required');
    
                    if ( $this->form_validation->run() == false )
                    {
                              echo validation_errors();
                    }
                    else
                    {
                              // Build the array after the form validation
                              $data = array(
                                        'email' => $this->input->post('email'), // POST, not GET
                                        'password' => $this->input->post('password')
                              );
    
                              // Load your model
                              $this->load->model('users_model');
                              if ( $this->users_model->insert($data) )
                              {
                                        echo 'Registration successful';
                              }
                              else
                              {
                                        echo 'Registration failed';
                              }
                    }
          }
    

    您还加载了表单助手,但您没有使用它。它使构建表单变得更加容易。

    http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

    <?php echo form_open('users/sportappregister'); ?>
    

    【讨论】:

    • 使用 post 而不是 get 并且控制器没有收到我的输入值!是否有必要使用表单助手才能使帖子正常工作,因为我真的不喜欢使用它?
    • 这可能是因为您的表单未设置为 POST。为什么你在使用表单助手时感觉不舒服?我在所有 CI 应用程序中都使用它,没有任何问题。
    • 从现在开始我会使用它,我的问题也没有了。谢谢你们的帮助!
    猜你喜欢
    • 2016-02-14
    • 2011-07-08
    • 1970-01-01
    • 2016-12-09
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多