如何在Codeigniter中获取所有Flash消息?

时间:2017-06-27 13:08:48

标签: php codeigniter validation

我有许多字段,其中有许多来自codeigniter验证的验证。但现在它显示一个单一的验证错误闪存,这是真正验证的消息

这是我的控制器块代码

public function customer_upd()
{
    extract($_POST);
    $err = '';

    $date1 = $_POST['cust_upd_bd'];
    $date2 = $_POST['cust_upd_dom'];

    if($date2 != "") {
        $datediff = (strtotime($date2) - strtotime($date1));
        $res = floor($datediff / (60 * 60 * 24));
        if ($res == " ") {
            $err .= "Please Enter a Date";
        } elseif ($res < 365) {
            $err .= "Child marriage is prohibited";
        }
        if ($err != '') {
            $_SESSION['dateerror'] = $err;
            redirect('customer/edit/'.$_POST['ctmr_upd_id']);
        }
    }
    $original_value1 = $this->db->query("SELECT customer_email FROM customer WHERE id = ".$_POST['ctmr_upd_id'])->row()->customer_email ;
    if($_POST['cust_upd_email'] != $original_value1) {
        $this->session->set_flashdata('add_failed','Email id must be unique & valid email address.');
        $is_unique =  '|is_unique[customer.customer_email]';
    } else {
        $is_unique =  '';
    }
    $original_value2 = $this->db->query("SELECT customer_mobile FROM customer WHERE id = ".$_POST['ctmr_upd_id'])->row()->customer_mobile ;
    if($_POST['cust_upd_mobile'] != $original_value2) {
        $this->session->set_flashdata('add_failed','Mobile no must be unique & exact 10 digit numeric length. ');
        $is_phoneunique =  '|is_unique[customer.customer_mobile]';
    } else {
        $is_phoneunique =  '';
    }
    $this->form_validation->set_rules('cust_upd_email', 'Email', 'required|valid_email|trim'.$is_unique);
    $this->form_validation->set_rules('cust_upd_mobile', 'Mobile', 'required|exact_length[10]|is_natural|trim'.$is_phoneunique);

    $this->form_validation->set_rules('cust_upd_name', 'Customer name', 'trim|required|min_length[3]|callback_customAlpha');
    $this->session->set_flashdata('cust','The Customer field must be at least 3 characters in length. ');
    $this->form_validation->set_rules('cust_upd_bd', 'Date of birth', 'regex_match[(0[1-9]|1[0-9]|2[0-9]|3(0|1))-(0[1-9]|1[0-2])-\d{4}]');
    $this->form_validation->set_rules('cust_upd_dom', 'Date of marriage', 'regex_match[(0[1-9]|1[0-9]|2[0-9]|3(0|1))-(0[1-9]|1[0-2])-\d{4}]');
    if ($this->form_validation->run() ) {
        $userdata = $this->session->userdata();
        $userId = $userdata['id'];
        if (!$userId):
            redirect(site_url());
        endif;

        extract($_POST);
        print_r($_POST); exit;
        $data = array(
            'customer_name' => ucwords($_POST['cust_upd_name']),
            'birth_date' => $_POST['cust_upd_bd'],
            'anniversery_date' => $_POST['cust_upd_dom'],
            'customer_mobile' => $_POST['cust_upd_mobile'],
            'customer_email' => $_POST['cust_upd_email'],
            'status' => $_POST['cust_upd_status'],
            'address' => $_POST['ctmr_address'],
            'cat_type' => $_POST['file_cat']
        );
        $userdata = $this->session->userdata();
        $userId = $userdata['id'];
        $this->db->where('user_id', $userId);
        $this->db->where('id', $_POST['file_cat']);
        $this->db->where('cat_status', 'Enable');
        $get_file = $this->db->get('category');
        $res_file = $get_file->num_rows();

        if($res_file >0){
            $userdata = $this->session->userdata();
            $userId = $userdata['id'];
            $this->db->where('user_id', $userId);
            $this->db->where('id', $_POST['ctmr_upd_id']);
            $ctmr_upd = $this->db->update('customer', $data);
        } else {
            $this->session->set_flashdata('edit_failed','Something went wrong.');
            redirect('customer/edit/'.$_POST['ctmr_upd_id']);
        }
        redirect(site_url() . '/customer');
    } else {
        redirect('customer/edit/'.$_POST['ctmr_upd_id']);
    }
}

这是我的观看代码

<div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <?php if($error = $this->session->flashdata('add_failed')): ?>
                <div class="row">
                    <div class="col-lg-9">
                        <div class="alert alert-dismissible alert-danger  col-md-offset-4">
                            <?php echo $error; ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php if($error1 = $this->session->flashdata('edit_failed')): ?>
                <div class="row">
                    <div class="col-lg-9">
                        <div class="alert alert-dismissible alert-danger  col-md-offset-4">
                            <?php echo $error1; ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php if($error2 = $this->session->flashdata('cust')): ?>
                <div class="row">
                    <div class="col-lg-9">
                        <div class="alert alert-dismissible alert-danger  col-md-offset-4">
                            <?php echo $error2; ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php if($_SESSION['dateerror']){
                echo '<div class="alert alert-danger col-sm-7 col-md-offset-3" role="alert">'.$_SESSION['dateerror'].'</div>';
                unset($_SESSION['dateerror']);
            }
            ?>

这是一个快照

enter image description here

每当我尝试使用他们的验证数据提交时,它会显示上面的错误消息。

我尝试过多次,但无法取得任何成功。

请指导我并告诉我哪里出错了?

1 个答案:

答案 0 :(得分:0)

使用下面提到的代码检索所有flashdata。

$this->session->flashdata();

它将返回包含所有flashdata的数组,只需省略关键参数。

如果不起作用,请告诉我。