CodeIgniter form_validation 未显示错误

Posted

技术标签:

【中文标题】CodeIgniter form_validation 未显示错误【英文标题】:CodeIgniter form_validation not showing errors 【发布时间】:2013-02-02 01:34:42 【问题描述】:

我的表单验证错误停止工作,昨天还在工作,我一定做错了什么但似乎找不到。

当我填写用户名和电子邮件地址时,它会发送电子邮件并回显“电子邮件已发送!”。

当我点击注册而不填写任何信息时,它只会重定向到加载视图的用户/注册。

控制器/user.php:

public function signup_validation() 
    $this -> load -> library('form_validation');

    $this -> form_validation -> set_rules('username', 'Username', 'trim|required|alpha|min_length[3]|max_length[25]');
    $this -> form_validation -> set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]');
    $this -> form_validation -> set_rules('password', 'Password', 'required|trim');
    $this -> form_validation -> set_rules('cpassword', 'Retype', 'required|trim|matches[password]');

    if ($this -> form_validation -> run()) 
        $key = md5(uniqid());

        $this -> load -> library('email', array('mailtype' => 'html'));
        $this -> load -> model('users');
        $this -> email -> from('', "");
        $this -> email -> to($this -> input -> post('email'));
        $this -> email -> subject('Confirm your account.');
        $message = "<p>Thank you for signing up!</p>";
        $message = "<p><a href='" . base_url() . "user/activate/$key'>Click here</a> to confirm your account.</p>";
        $this -> email -> message($message);
        if ($this -> users -> add_temp_user($key)) 
            if ($this -> email -> send()) 
                echo "The email has been sent!";
             else 
                echo "Could not send the email.";
            
         else 
            echo "Problem adding user to database.";
        
     else 
        redirect('user/signup');
    

views/signup.php

<form class="form-horizontal" action="<?php echo base_url() . 'user/signup_validation'; ?>" method="post" accept-charset="UTF-8">
    <?php echo validation_errors(); ?>
    <div class="control-group">
        <label class="control-label" for="inputEmail">Username</label>
        <div class="controls">
            <input name="username" type="text" id="inputUsername" placeholder="username" value="<?php echo $this -> input -> post('username'); ?>"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="inputEmail">Email</label>
        <div class="controls">
            <input name="email" type="email" id="inputEmail" placeholder="email" value="<?php echo $this -> input -> post('email'); ?>"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="inputPassword">Password</label>
        <div class="controls">
            <input name="password" type="password" id="inputPassword" placeholder="password" />
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="inputRetype">Retype</label>
        <div class="controls">
            <input name="cpassword" type="password" id="inputRetype" placeholder="retype" />
        </div>
    </div>
    <div class="form-actions">
        <input name="signup_submit" type="submit" class="btn" value="Sign up" />
    </div>
</form>

我自动加载帮助程序:

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

.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> 

【问题讨论】:

我是不是读错了什么,老实说,如果表单验证 = false,你看起来就成功了。重定向不适用于表单验证,您需要重新加载视图而不是重定向到它。 如果验证没有失败,您不应该将用户添加到数据库吗? 当您加载视图而不将其重定向到某个页面时会显示验证错误。尝试加载注册页面的视图,而不是重定向。如果有帮助,请告诉我。 是的,如果验证没有失败,就将用户添加到数据库中,愚蠢的我。这也没有解决问题。并且 redirect('user/signup') 加载函数 signup() 加载我的页眉、菜单栏、内容和页脚。加载视图直接解决了问题。谢谢! 我将其添加为答案。如果有帮助,请投票并打勾。谢谢。 【参考方案1】:

$this-&gt;load-&gt;view('sign_up');代替redirect('user/signup');

当您加载视图并且不将其重定向到加载该页面的控制器方法时会显示验证错误。

【讨论】:

以上是关于CodeIgniter form_validation 未显示错误的主要内容,如果未能解决你的问题,请参考以下文章

调用未定义函数 codeigniter\locale_set_default() 时出现 codeigniter 错误

codeigniter 中是不是有子查询库?或者我如何在codeigniter 3中进行子查询?

php优秀框架codeigniter学习系列——CodeIgniter.php概览

Codeigniter- Ajax Codeigniter Ajax - 通过ajax返回不止一行

如何在codeigniter中集成swfupload

CodeIgniter 4 项目中未定义的常量“CodeIgniter\Database\MySQLi\MYSQLI_STORE_RESULT”