php - CodeIgniter 表单验证不工作

标签 php html validation codeigniter webforms

似乎无法在 code igniter 上进行表单验证,不确定哪里出了问题!

这是我的 Controller 类:

class registerController extends MY_Controller {

    // --- Methods -----------------

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

        $this->firephp->log('REGISTER PAGE CONTROLLER ACTIVE');

        //Load user model
        $this->load->model('usersModel');

        //load form validation
        $this->load->helper('form');
        $this->load->library('form_validation');

    }

    //------------------------------

    public function register()
    {

        $this->form_validation->set_rules('registerForenameFieldName', 'Fist Name', 'required');

        //This is executed when the form is submitted
        if ($this->form_validation->run() == FALSE)
        {

            $this->firephp->log('Registration form validation failed');

            //Load in the views
            $this->load->view('global/head.php');
            $this->load->view('global/top.php');
            $this->load->view('register/register.php');
            $this->load->view('global/footer.php');

        } else {

            $this->firephp->log('Registration form validation succeeded');

            //Model method here

            //Load in the views
            $this->load->view('global/head.php');
            $this->load->view('global/top.php');
            $this->load->view('home.php');
            $this->load->view('global/footer.php');
        }

    }

}

和我的表格:

<?php  echo form_open('register'); ?>

    <div id="registerErrors"><?php echo validation_errors(); ?></div>

        <table id="registerTable">

            <tr>
            <td class="regLeftCell"><p>First Name</p></td>
            <td class="regRightCell">
            <input id="registerForenameField" class="textField registerField" name="registerForenameFieldName" type="text" placeholder="Forename" value="<?php echo set_value('registerForenameFieldName'); ?>"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Last Name</p></td>
            <td class="regRightCell">
            <input id="registerSurnameField" class="textField registerField" name="registerSurnameFieldName" type="text" placeholder="Surname" value="<?php echo set_value('registerSurnameFieldName'); ?>"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Email Address</p></td>
            <td class="regRightCell">
            <input id="registerEmailField" class="textField registerField" name="registerEmailFieldName" type="text" placeholder="Email Address"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Choose Password</p></td>
            <td class="regRightCell">
            <input id="registerPasswordField" class="textField registerField" name="registerPasswordFieldName" type="text" placeholder="Password"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Confirm Password</p></td>
            <td class="regRightCell">
            <input id="registerConfirmPasswordField" class="textField registerField" name="registerConfirmPasswordFieldName" type="text" placeholder="Confirm Password"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Address Line 1</p></td>
            <td class="regRightCell">
            <input id="registerAddress1Field" class="textField registerField" name="registerAddress1FieldName" type="text" placeholder="Address Line 1"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Address Line 2</p></td>
            <td class="regRightCell">
            <input id="registerAddress2Field" class="textField registerField" name="registerAddress2FieldName" type="text" placeholder="Address Line 2"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Address Line 3</p></td>
            <td class="regRightCell">
            <input id="registerAddress3Field" class="textField registerField" name="registerAddress3FieldName" type="text" placeholder="Address Line 3"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Post Code</p></td>
            <td class="regRightCell">
            <input id="registerAddressPostCodeField" class="textField registerField" name="registerPostCodeFieldName" type="text" placeholder="Post Code"></input>
            </td>
            </tr>

            <tr>
            <td class="regLeftCell"><p>Country</p></td>
            <td class="regRightCell">
            <input id="registerAddressCountryField" class="textField registerField" name="registerSurnameFieldName" type="text" placeholder="Country"></input>
            </td>
            </tr>

        </table>

        <div id="registerButton">
            <input class="button registerSubmitButton" type="submit" value="Register"/>
        </div>


    </form>

路由是这样设置的:

$route['register'] = 'registerController/register';

怎么了?它只是每次都失败而不显示错误。

它只是重新加载相同的页面而没有验证错误。 $this->form_validation->run() 每次都评估为 FALSE,因为我已经将它记录在 FirePHP,一个 Firebug 扩展中。

编辑:

可能还值得一提的是,尽管它加载了页面,但我在控制台中收到了 404 错误:

other error

最佳答案

我终于找到了答案!我的服务器配置不正确,我没有启用 mod_rewrite,所以所有请求都无法正常工作。

Ubuntu 用户,在命令行上关闭它以启用它: sudo a2enmod 重写

然后重启Apache。

我正在使用以下 .htaccess:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 /index.php
</IfModule>

关于php - CodeIgniter 表单验证不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11493479/

相关文章:

html - 如何在 Bootstrap 行中垂直居中文本

asp.net - 如何验证列表框不为空(客户端)

作为 Cron 的 PHP 脚本不起作用,但可以从 CLI 运行

javascript - 如何让按钮用ajax运行php代码,而不刷新页面 购物车

php - 我坚持我的 SELECT sql 查询

html - 我想在点击登录按钮后导航到另一个页面

php - 代码不打印数据库中的值

javascript - 一个html输入类型如何调用两个函数?

java - Hibernate 验证 没有验证

php - 寻找可以测试其他人的成人图像的裸体图像过滤器