我的错误没有显示在我的表单上? form_validation?

时间:2014-12-18 16:37:28

标签: php codeigniter

这是我的控制器代码:

public function register()
{
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('register_model');
$this->load->library('form_validation');    
$this->load->view('head');
$this->load->view('register');
$this->load->view('footer');

$config = array(
           array(
                 'field'   => 'username', 
                 'label'   => 'Username', 
                 'rules'   => 'trim|required|min_length[5]|max_length[12]|xss_clean'
              ),
           array(
                 'field'   => 'password', 
                 'label'   => 'Password', 
                 'rules'   => 'trim|required|matches[passconf]|md5'
              ),
            array(
                 'field'   => 'passconf', 
                 'label'   => 'Password Confirmation', 
                 'rules'   => 'trim|required'
              ),
           array(
                 'field'   => 'email', 
                 'label'   => 'Email', 
                 'rules'   => 'trim|required|valid_email'
              )
        );
$this->form_validation->set_message('required', 'Field required');
$this->form_validation->set_rules($config);


if ($this->form_validation->run() === FALSE)
{
$this->load->view('head');
$this->load->view('register');
$this->load->view('footer');
}
else
{
$this->register_model->set_user();
redirect ('login', 'refresh');
}
}

这是我的观看代码:

<div id="regform" class="one-third column">
<h2>Register</h2>

<?php echo validation_errors();

 echo form_open('register'); ?>

    <label for="username">Username</label>
    <?php echo form_error('username'); ?>
    <input type="text" name="username" value"<?php echo set_value('username'); ?>"/>

    <label for="password">Password</label>

    <input type="password" name="password" value"<?php echo set_value('password'); ?>"/>

    <label for="passconf">Password Confirmation</label>

    <input type="password" name="passconf" value"<?php echo set_value('passconf'); ?>" />

    <label for="email">Email</label>
<?php echo form_error('email'); ?>
    <input type="text" name="email" value"<?php echo set_value('email'); ?>"/></br>


        <input type="submit" name="submit" value="Register" />

    </form>

    </div>

表单规则适用,但是当我做错了什么时,不会出现其中一个错误消息。有什么想法吗?我已经查看了所有其他问题,但没有人对我的答案。感谢

1 个答案:

答案 0 :(得分:1)

问题是,在您的Controller中,您已经在设置验证之前加载了View。

public function register()
{

    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->model('register_model');
    $this->load->library('form_validation');

    $this->load->view('head');     // <- too early
    $this->load->view('register'); // <- too early
    $this->load->view('footer');   // <- too early

    // nothing below here is going to work because you've already loaded the page

    ....

您需要重新设计您的控制器,以便始终为每种情况加载您的视图。