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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办