3

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

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

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

控制器/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');
    }
}

意见/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');

.ht 访问:

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

1 回答 1

16

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

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

于 2013-02-17T15:20:22.817 回答