【问题标题】:Codeigniter not displaying validation_errors()Codeigniter 不显示验证错误()
【发布时间】:2014-04-03 01:18:54
【问题描述】:

解决方案:我在我的 VPS 上使用了 WAMP 服务器。问题是重写模块未在 apache 设置中加载。我通过进入我的 apache 文件夹/conf/http.conf 并删除此行前面的主题标签来解决此问题:LoadModule rewrite_module modules/mod_rewrite.so。我重新启动了 apache,它工作了。

编辑:即使我输入了电子邮件和密码,它也没有运行。我似乎根本无法运行 form_validation。

编辑 2:还发现我无法回显 $this->input->post('email');在函数 login_validation 中。

编辑 3:我回显了 $this->output->enable_profiler();它说我没有发送任何帖子数据。我的 .htaccess 是否有可能在发布内容时有所作为?

我正在使用 codeigniter 创建登录/注册系统。我让它在本地主机上工作,但是当我把它移到我的主机时一定发生了一些事情。

我在我的 autoload.php 中自动加载表单:

$autoload['helper'] = array('form', 'url');

这是我在主文件控制器/main.php 类中的验证代码:

public function login_validation(){

    $this->load->library('form_validation');

    $this->form_validation->set_rules('email', 'Email', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required|md5');

    if($this->form_validation->run()){
        //NO VALIDATION ERRORS
    }else{
        echo 'Errors:<br />';
        echo validation_errors();
    }
}

我的表格:

<?php 
echo form_open('main/login_validation');

$property_type = array('class' => 'bar left', 'value' => 'E-Mail', 'name' => 'email');
echo form_input($property_type);

$property_type = array('class' => 'bar right', 'value' => 'Password', 'name' => 'password');
echo form_password($property_type);

$property_type = array('class' => 'buttonone', 'value' => 'Login', 'name' => 'login_submit');
echo form_submit($property_type);

echo form_close(); 
?>

当我没有在表单中输入任何内容时,它应该显示电子邮件和密码是必需的。它只说错误:。但它不显示验证错误。

当我将它上传到我的主机时,我还必须更改为另一个 .htaccess。

这是我的新 htaccess:

  <IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at
  #  http://www.example.com/mypage/test1
  # then use
  # RewriteBase /mypage/test1/
  RewriteBase /
  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.
  # Submitted by: ElliotHaughin

  ErrorDocument 404 /index.php
</IfModule>

这是我的旧 .htaccess:

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
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]    

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    只需检查if条件并将表单错误功能放入查看页面

    公共函数 login_validation(){

        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|md5');
    
          if ($this->form_validation->run($this) !== FALSE) //write like this if condition
          { 
            //NO VALIDATION ERRORS
        }else{
            echo 'Errors:<br />';
            echo validation_errors();
        }
    }
    

    您的视图页面放入此

    <?php echo validation_errors(); ?>
    

    更多请参考http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

    【讨论】:

      【解决方案2】:

      我在我的 VPS 上使用了 WAMP 服务器。问题是重写模块未在 apache 设置中加载。我通过进入我的 apache 文件夹/conf/http.conf 并删除此行前面的主题标签来解决此问题:LoadModule rewrite_module modules/mod_rewrite.so。我重新启动了 apache,它工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-11
        • 2017-01-25
        • 1970-01-01
        • 2016-02-18
        • 1970-01-01
        • 2017-10-13
        • 2014-02-20
        相关资源
        最近更新 更多