CodeIgniter中未显示验证flashdata错误

时间:2017-05-14 17:50:15

标签: php codeigniter session flash-message

这是我在控制器内部的登录方法,这里我为验证错误设置了一条flash消息 -

public function login(){
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[3]');

    if($this->form_validation->run() == FALSE){
        $data = array(
          'errors' => validation_errors()
        );
        $this->session->set_flashdata($data);
        redirect('home');
    }
}

这里标识显示这些错误的代码 -

<?php if($this->session->flashdata('errors')): ?>
    <?php echo $this->session->flashdata('errors');?>

<?php endif; ?>

我不知道出了什么问题,错误信息没有显示。

2 个答案:

答案 0 :(得分:0)

 i hope work this process
if($this->form_validation->run() == FALSE){
      $this->session->set_flashdata('name','Your message');
        redirect('home');
        }
    <?php if($this->session->flashdata('name')): ?>
        <?php echo $this->session->flashdata('name');?>

    <?php endif; ?>

答案 1 :(得分:0)

将其置于控制器中

if ($this->form_validation->run() == FALSE) {       
$errors = validation_errors();
           $this->session->set_flashdata('form_error', $errors);
           $this->load->view('Your View page');
}

将其放置在您的视图页面

<?php if($this->session->flashdata('form_error')): ?>
    <?php echo $this->session->flashdata('form_error');?>

<?php endif; ?>

我希望这会对你有所帮助..!