简体   繁体   中英

how to validate edit form in codeigniter

I am trying to validate my edit form before send the data but I get this error

Undefined offset: 0

It seems like it is unable to get id. How can I solve this problem?
that is my code

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

Thats how I do it.

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

}

Function in Admin Model to Update the record

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

Set your rules in model.

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

}

Then use this rule in your controller as below. Also you must load Form Validation Library in your controller or 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');
   }

Finally show validation errors in your view. Add below code to your edit_papers.php file.

<?php echo validation_errors(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM