du8791069 2016-06-27 10:16
浏览 52
已采纳

Codeigniter表单验证不显示错误

I have a problem, my codeigniter is not displaying error messages.

When I do a var_dump on $this->form_validation->run(), it returns true if everything is completed and false if not.

Here is the code:

Controller

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

}

View

  <?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();
                    ?>

Apparently it's all right, but I can not find the error. If the form is completed and the registered user, it will be redirected to the home page, if not back to the login screen, but without displaying the error.

Can you help me?

展开全部

  • 写回答

1条回答 默认 最新

  • donglv7097 2016-06-27 10:25
    关注

    Change this to (Best Practices)

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

    this

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

    Since you haven’t told the Form Validation class to validate anything yet, it returns FALSE (boolean false) by default. The run() method only returns TRUE if it has successfully applied your rules without any of them failing.

    Check Why


    Add this in controller

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

    and remove this

    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


    Add this in form below the respective input fields

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

    Or add

    <?php echo validation_errors(); ?>
    

    Check Why


    CodeIgniter Form Validation Example

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

报告相同问题?

悬赏问题

  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部