如何在codeigniter中验证编辑表单

时间:2017-01-08 07:29:33

标签: php codeigniter

我在发送数据之前尝试验证我的编辑表单,但是我收到此错误

  

未定义的偏移量:0

似乎无法获得身份证明。我该如何解决这个问题呢? 那是我的代码

public function update_Papers_view()
{
    $id = $this->uri->segment('3');
    $query = $this->db->get_where("papers", array("id" => $id));
    $data['sucss'] = $query->result();
    $data['id'] = $id;

    $this->load->view('Layout/menu');
    $this->load->view('Papers/edit_papers ', $data);
    $this->load->view('Layout/footer');
}

public function update_Papers()
{
    $this->load->model('PapersModel');

    $this->form_validation->set_rules('name_person', 'name_person', 'required', array('required' => 'الرجاء ادخال الاسم'));
    $this->form_validation->set_rules('name', 'name', 'required', array('required' => 'الرجاء ادخال اسم الورقه'));

    if ($this->form_validation->run() === false) {

        redirect('Paperscon/update_Papers_view');
    } else {
        $data = array(
            'name_person' => $this->input->post('name_person'),
            'name' => $this->input->post('name'),
            'type' => $this->input->post('type'),
            'date_paper' => $this->input->post('date_paper'),
            'address' => $this->input->post('address'),

        );
        $id = $this->input->post('id');
        $this->PapersModel->update($data, $id);
        $query = $this->db->get("papers");
        $data['sucss'] = $query->result();
        $this->load->view('Layout/menu');
        $this->load->view('Papers/show_papers', $data);
        $this->load->view('Layout/footer');
    }
}

2 个答案:

答案 0 :(得分:0)

多数民众赞成我是怎么做到的。

public function edit_admin_menu()
{
    if($this->isLoggedIn())
    {
        $menuId=$this->uri->segment(3);// Menu Item that needs to be Edited
        $data['menu']=$this->admin_model->getMenuItems();
        $data['menu_item']=$this->admin_model->getMenuItemDetail($menuId);// Get that Menu item

        if($_POST)
        {
            $config=array(
                array(
                    'field' =>  'parent',
                    'label' =>  'Parent',
                    'rules' =>  'trim|required'
                ),
                array(
                    'field' =>  'name',
                    'label' =>  'Name',
                    'rules' =>  'trim|required'
                )
            );
            $this->form_validation->set_rules($config); // Validation check
            if($this->form_validation->run()==false)
            {
                $data['errors']=validation_errors();// Errors in an array
                $data['parents']=$this->admin_model->getMenuParents();
                $data['menu_item']=$this->admin_model->getMenuItemDetail($menuId);
                //Load Views
            }
            else
            {
                $this->admin_model->updateMenuItem($_POST,$menuId);
                $data['success']='Congratulations! Menu Item Updated Successfully';
                $data['parents']=$this->admin_model->getMenuParents();
                $data['menu']=$this->admin_model->getMenuItems();
                $data['menu_item']=$this->admin_model->getMenuItemDetail($menuId);
                //Load Views
            }
        }
        else
        {
            $data['parents']=$this->admin_model->getMenuParents();
            //Load Views
        }
    }
    else
    {
        redirect(base_url());
    }

}

管理模型中更新记录的功能

public function updateMenuItem($data,$menuId)
{
    $item=array(
        'parent'=>$data['parent'],
        'name'=>$data['name'],
        'class'=>$data['class'],
        'url'=>$data['url']
    );

    $this->db->WHERE('id',$menuId)->update('admin_menu',$item);
}

答案 1 :(得分:0)

在模型中设置规则。

    class PapersModel extends CI_Model {

    public $rules = array(
        'name_person' => array(
            'field' => 'name_person',
            'label' => 'Person Name',
            'rules' => 'trim|required'
        ),
        'name' => array(
            'field' => 'name',
            'label' => 'Name',
            'rules' => 'trim|required'
        ),
    );

    public function __construct() {
        parent::__construct();
    }

}

然后在您的控制器中使用此规则,如下所示。您还必须在控制器或auotload.php中加载表单验证库

$this->load->library('form_validation');

 public function update_Papers(){ 
     $this->load->model('PapersModel');
     $rules = $this->PapersModel->rules;
     $this->form_validation->set_rules($rules);
     if($this->form_validation->run() == TRUE){
            // Your update query
        }
     $this->load->view('Layout/menu');
     $this->load->view('Papers/edit_papers ',$data);
     $this->load->view('Layout/footer');
   }

最后在您的视图中显示验证错误。将以下代码添加到edit_papers.php文件中。

<?php echo validation_errors(); ?>