验证错误不是net到codeigniter中的flashdata

时间:2018-08-21 03:28:34

标签: codeigniter codeigniter-session codeigniter-form-validation

我正在尝试在codeigniter中使用flashdata将验证错误设置为我的表单,但在视图中看不到错误,这是我的视图代码部分。我已经阅读过与该问题接近的其他相关问题,但找不到解决方案!

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

                <?php $attributes = array('id'=>'admin_login','class'=>'form-horizontal'); ?>
                <?php echo form_open('admin/login', $attributes); ?>
                    <div class="form-group">
                        <?php
                        $lbl = array('class'=>'col-sm-12 col-lg-12 col-md-12 col-xs-12 control-label');
                        echo form_label('Email','admin_login',$lbl); ?>
                        <div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
                            <?php $data = array('
                                class'=> 'form-control col-sm-12 col-lg-12 col-md-12 col-xs-12',
                                'name'=> 'email',
                                'placeholder'=>'Enter your email'
                            ); ?>
                            <?php echo form_input($data); ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <?php
                        echo form_label('Password','admin_login',$lbl); ?>
                        <div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
                            <?php $data = array('
                                class'=> 'form-control',
                                'name'=> 'password',
                                'placeholder'=>'Password'
                            ); ?>
                            <?php echo form_password($data); ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
                            <?php $data = array('
                                class'=> 'btn btn-success btn-md center mt-2',
                                'name'=> 'submit',
                                'placeholder'=>'Password',
                                'value'=>'LOGIN',
                                'type'=>'submit'
                            ); ?>
                            <center>
                                <?php echo form_submit($data); ?>
                            </center>
                        </div>
                    </div>
                <?php echo form_close(); ?>

这是我的控制器部分,它是“管理员”的名称,方法名称是“登录”

        public function login(){

            $this->form_validation->set_rules('email','Email','required');
            $this->form_validation->set_rules('password','Password','required');

            // check the form has any error (any miss of rules we set in above codes

                   if($this->form_validation->run() == FALSE){
                    $this->session->set_flashdata('errors', 
                    validation_errors());

    //              $this->session->set_flashdata('name','Your message');
    //              $errors = validation_errors();
    //              $this->session->set_flashdata('form_error', $errors);

    //              $this->session->set_flashdata('name','Your message');
    //              $data = array('errors'=> validation_errors());
    //              set_userdata() is regular way to set session, it need manually unset, there for we use flashdata
    //              $this->session->set_flashdata($data);
            }else{
                echo "its all good";
            }
        }

我尝试了很多代码,我注释掉了到目前为止使用的代码。我也在$autoload['libraries'] = array('database','form_validation','session');

中设置了自动加载部分会话库

请告诉我这是怎么回事。

1 个答案:

答案 0 :(得分:3)

请尝试以下代码。

public function login(){
    $this->form_validation->set_rules('email','Email','required');
    $this->form_validation->set_rules('password','Password','required');

    if($this->form_validation->run() == true){
       echo "its all good";
    }else{
       $this->session->set_flashdata('errors', validation_errors());
    }
    $this->load->view('Your View Page');
}