php - CodeIgniter form_validation 不显示错误

标签 php html forms codeigniter validation

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

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

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

controllers/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> 

最佳答案

$this->load->view('sign_up'); 替换为 redirect('user/signup');

当您加载 View 并且不将其重定向到加载该页面的 Controller 方法时,会显示验证错误。

关于php - CodeIgniter form_validation 不显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14922446/

相关文章:

php - YouTube API如何解决错误的请求错误400 PHP

php - 正则表达式在 javascript 中有效,但在 PHP 中无效

javascript - 需要React输入框为 float

python - 使用像 "Tamper Data"这样的 Python Mechanize

html - 如何只允许选中一个单选按钮?

php - .htaccess重写规则异常

javascript - 使用带有视频和音频标签的默认海报图像

Javascript element.style.backgroundImage 不加载图像

javascript - 是否可以停止从打印预览中复制文本?

javascript - 如何在提交后使 php 表单只读并在同一页面上单击编辑可编辑?