【问题标题】:CodeIgniter Form Validation Not WorkingCodeIgniter 表单验证不起作用
【发布时间】:2012-07-14 15:52:31
【问题描述】:

似乎无法在代码点火器上进行表单验证,不确定出了什么问题!

这是我的控制器类:

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 错误:

【问题讨论】:

  • 您是否尝试更改最后一个输入的名称,也就是registerSurnameFieldName
  • 啊,很好看,但不是这样!快把我逼疯了,我这辈子都看不出有什么问题!
  • 当您将required 更改为trim 之类的内容时是否有效?

标签: php html validation codeigniter webforms


【解决方案1】:

我不明白为什么人们要在 firebug 中加载用于打印变量和数组的扩展。对于我使用这个的大多数项目:是一样的。你不需要firephp核心。而且你不需要firebug的firephp插件。

<?php
$debug[] = 'Hello World';
// and/or
$debug[] = array('a1'=>"ArrValue1",'a2'=>"ArrValue2");
?>

<!-- add before </body>... -->
<?php if(isset($debug)): ?>
<script type="text/javascript">
jQuery(document).ready(function(){ 
    <?php foreach($debug as $js): if(is_array($js)) $js= json_encode($js); else $js = '"'.$js.'"'; ?>
    console.log(<?php echo $js; ?>);
    <?php endforeach; ?>
})
</script>
<?php endif; ?>

【讨论】:

    【解决方案2】:

    将此行添加到您的 __construct:

    $this->load->helper('url');
    

    然后试试这个:

    echo form_open(site_url("register"));
    

    函数site_url 可以帮助你使用路由的url。 如果这没有任何帮助,您可以尝试 echo base_url();和其他 url 辅助函数来完成 url。

    【讨论】:

      【解决方案3】:

      我终于找到答案了!我的服务器配置不正确,我没有启用 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>
      

      【讨论】:

      • 这解决了我的问题,所有 GET/POST 请求都得到 404,我有一个类似的 .htaccess。 a2enmod rewrite 启用了 mod_rewrite,一切都很好,谢谢。
      • 我试过了,但每次请求都会收到 404 错误
      【解决方案4】:

      你没有在form_open函数中给出控制器名称,应该是

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

      文档: Form Helper.

      【讨论】:

      • 没有运气就试过了。无论如何,路由不应该为我解决吗? $route['register'] = 'registerController/register';
      • 你确定控制器被执行了吗?尝试一些方法以确保您的控制器/构造函数正在执行。
      • 是的,正在执行表单验证功能,因为我放了 $this->firephp->log('注册表单验证失败');在 $this->form_validation->run() == FALSE 条件下,每次都会被记录。
      • 不确定但尝试添加另一条规则为xss_clean|required
      • 不走运,我认为这与图片上萤火虫中的 404 有关。不过不知道。非常卡住
      【解决方案5】:
      1. 这是什么意思 - “每次都失败” - 显示空白页面?还是错误?还是什么?

      2. 构造函数正在执行吗?

        function __construct()
        {
            parent::__construct();
            var_dump('contructor');
        }
        
      3. 是否调用了注册函数?

        function register()
        {
            var_dump('register function');
        }
        

      或者只是添加

      $this->firephp->log('Registration function');
      

      register函数的开头,让我们知道你的调试结果。

      您是否尝试过这里的示例:

      http://codeigniter.com/user_guide/libraries/form_validation.html

      【讨论】:

      • 是的,所有人都被召唤了。它只是重新加载同一页面而没有验证错误。 $this->form_validation->run() 每次都评估为 FALSE。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2020-09-28
      • 2012-02-26
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多