Codeigniter表单验证不显示错误

时间:2016-06-27 18:16:02

标签: php html forms codeigniter validation

我遇到问题,我的codeigniter没有显示错误消息。

当我在var_dump上执行$this->form_validation->run()时,如果一切都已完成则返回true,否则返回false。

以下是代码:

控制器

defined('BASEPATH') OR exit('No direct script access allowed');

class Logon extends CI_Controller {
public function __construct() {
      parent::__construct();
      $this->load->model('logon_model', 'logon');
      }

public function autenthicate() {
        $this->form_validation->set_rules('usuario', 'Usuario', 'required');
        $this->form_validation->set_rules('senha', 'Senha', 'required');

        if ($this->form_validation->run() == TRUE):
            #Array with data
            $data = elements(array('user', 'password'), $this->input->post());
            $data['password'] = md5($data['password']);

            #Send data to model
            if ($this->logon->do_login($data)->result()):

                $data = $this->logon->do_login($data)->result();

                $user_data = array(
                    'id' => $data['id'],
                    'name' => $data['name'],
                    'logged' => TRUE
                );

                $this->session->set_userdata($user_data);
                redirect('sales');

                $this->session->set_flashdata('loginerror', 'Login failed! User or password is invalid.');
                redirect('logon');
            endif;
        else:
            $data = array(
                'title' => 'Logon',
                'screen' => 'index',
            );

            $this->load->view('index_view', $dados);
        endif;
    }

}

查看

  <?php
                    echo form_open('logon/autenthicate');
                    echo '<div class="form-group">';
                    echo form_label("User");
                    echo form_input(array('name' => 'user', 'class' => 'form-control'));
                    echo '</div>';

                    echo '<div class="form-group">';
                    echo form_label("passwor");
                    echo form_password(array('name' => 'password', 'class' => 'form-control'));
                    echo '</div>';

                    echo '<div class="form-group">';
                    echo form_submit('logon', 'Logon', 'class="btn btn-default btn-block"');
                    echo '</div>';

                    #Show a alert div with validation errors
                    echo validation_errors('<div class="alert alert-danger alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>', '</div>');
                    echo form_close();
                    ?>

显然没关系,但我找不到错误。如果表单已完成并且已注册用户,则会将其重定向到主页,如果没有返回登录屏幕,但不显示错误。

你能帮助我吗?

1 个答案:

答案 0 :(得分:2)

更改为( 最佳做法

if ($this->form_validation->run() == TRUE)

if ($this->form_validation->run() == FALSE)
  

由于您还没有告诉Form Validation类验证任何内容,因此默认情况下它返回FALSE(布尔值false)。 The run()方法只有在成功应用规则且没有任何失败的情况下才会返回TRUE。

Check Why

在控制器中添加

$this->form_validation->set_error_delimiters('<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>', '</div>');

删除此

echo validation_errors('<div class="alert alert-danger alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>', '</div>');

Check Why

在相应输入字段下方的表单中添加

echo form_error('usuario');
echo form_error('senha'); 

或添加

<?php echo validation_errors(); ?>

Check Why

CodeIgniter Form Validation Example