douwa0280
2014-12-18 16:37
浏览 49
已采纳

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

Here is my controller code:

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');
}
}

Here is my view code:

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

The form rules apply, however when i do something wrong, not one of the error messages show up. Any thoughts? I have looked at all other questions but none have an answer to mine. Thanks

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyao1856 2014-12-18 16:51
    已采纳

    The problem is that, in your Controller, you've loaded your View before you've set up the validation.

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

    You'll need to rework your Controller so that loading your View is always done last for every condition.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题