无法显示codeigniter验证错误

时间:2017-06-22 09:53:25

标签: php codeigniter

在这里,我使用codeIgniter form_validation验证客户名称,这可以防止数字和特殊字符以及NULL。

我使用了一个回调函数,它只允许在空格中存储字母字符,因为名字总是包含空格。

现在的问题是,当验证失败时,它无法打印错误消息。

这是我的回调函数

 public function customAlpha($str)
    {
        if ( !preg_match('/^[a-z .,\-]+$/i',$str) )
        {
            $this->form_validation->set_message('customAlpha', 'The {field} field may only contain alphabetical characters.');
            return false;
        } else {
            return True;
        }
    }

这是我的控制器代码

    public function customer_upd()
    {
        $original_value = $this->db->query("SELECT customer_email FROM customer WHERE id = ".$_POST['ctmr_upd_id'])->row()->customer_email ;
        if($_POST['cust_upd_email'] != $original_value) {
            $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_value = $this->db->query("SELECT customer_mobile FROM customer WHERE id = ".$_POST['ctmr_upd_id'])->row()->customer_mobile ;
        if($_POST['cust_upd_mobile'] != $original_value) {
            $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', 'trim|required|min_length[3]|callback_customAlpha');

        if ($this->form_validation->run() ) {
            $userdata = $this->session->userdata();
            $userId = $userdata['id'];
            if (!$userId):
                redirect(site_url());
            endif;
            $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="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Name <span
                                        class="required">*</span>
                            </label>
                            <?php
                            foreach ($qry as $res_sel_qry){
                            ?>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input id="onlyname" class="form-control col-md-7 col-xs-12" name="cust_upd_name"
                                       placeholder="" value="<?php echo $res_sel_qry['customer_name']; ?>"
                                       required="required" type="text">
                            </div>
                            <?php echo form_error('cust_upd_name'); ?>
                        </div>

有许多事情需要验证,并且正常工作需要客户名称(不会显示错误消息)。

请指导我,我哪里出错了?感谢。

2 个答案:

答案 0 :(得分:0)

删除重定向功能

if ($this->form_validation->run() ) {
        $userdata = $this->session->userdata();
        $userId = $userdata['id'];
        if (!$userId):
            redirect(site_url());
        endif;
        $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']);
    }

答案 1 :(得分:0)

替换此

&#xA;&#xA;
 &lt; div class =“col-md-6 col-sm-6 col-xs-12”&gt;&#xA ;  
&#xA;&#xA;

使用

&#xA;&#xA;
 &lt; div class =“&lt;?php if (form_error('cust_upd_name')!= null){echo'has-error';}?&gt;“&gt;&#xA;  
&#xA;&#xA;

In你的css

&#xA;&#xA;
  .has-error {&#xA; color:red;&#xA;}&#xA;  
&#XA;