waktu tersisa untuk klaim DISKON 40% dalam program Full Stack Membership. Klaim Promo!

terjadi error "Undefined method 'getValidationRules'

Ditanyakan pada tanggal 3 June 2020 oleh Ramadhan Gamasmara Edit  Hapus  Tandai Selesai  Tandai 

terjadi error yaitu "Undefined method 'getValidationRules'.

berikut script code dari file yg saya namai "my_model.php"

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class my_model extends CI_Model {

    protected $table    = '';
    protected $perPage  = 5;

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

        if (!$this->table){
            $this->table = strtolower(
              str_replace('_model', '', get_class($this))  
            );
        }

    }
    /**
     * Fungsi Validasi input
     * Rules: Dideklarasi dalam masing-masing model
     */
    public function validate(){

        $this->load->library('form_validation');
        $this->form_validation->set_error_delimeters(
            '<small class="form-text text-danger">', '</small>'
        );

        $validationRules = $this->getValidationRules();

        $this->form_validation->set_rules($validationRules);

        return $this->form_validation->run();
    }

    public function select($columns){

        $this->db->select($columns);
        return $this;
    }

    public function where($column, $condition){

        $this->db->where($column, $condition);
        return $this;
    }

    public function like($column, $condition){
        $this->db->like($column, $condition);
        return $this;
    }

    public function orLike($column, $condition){
        $this->db->or_Like($column, $condition);
        return $this;
    }

    public function join($table, $type = 'left'){
        $this->db->join($table, "$this->table.id_$table = $table.id", $type);
        return $this;
    }

    public function orderBy($column, $order = 'asc'){
        $this->db->order_by($column, $order);
        return $this;
    }

    public function first(){
        return $this->db->get($this->table)->row();

    }

    public function get(){
        return $this->db->get($this->table)->result();
    }

    public function count(){
        return $this->db->count_all_results($this->table);
    }

    public function create($data){
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function update($data){
        return $this->db->update($this->table, $data);
    }

    public function delete(){
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }

    public function paginate($page){
        $this->db->limit(
            $this->perPage,
            $this->calculateRealOffset($page)
        );
    }

    public function calculateRealOffset($page){
        if (is_null($page) || empty($page)) {

            $offset = 0;
        }else{
            $offset = ($page * $this->perPage) -$this->perPage;
        }
        return $offset;
    }

    public function makePagination($baseUrl, $uriSegment, $totalRows = null){
        $this->load->library('pagination');

        $config = [
            'baseUrl'           =>  $baseUrl,
            'uri_segment'       =>  $uriSegment,
            'per_page'          =>  $this->perPage,
            'total_rows'        =>  $totalRows,
            'use_page_numbers'  =>  true,

            'full_tag_open'     =>  '<ul class="pagination">',
            'full_tag_close'    =>  '</ul>',
            'attributes'        =>  ['class' => 'page-link'],
            'first_link'        =>  false,
            'last_link'         =>  false,
            'first_tag_open'    =>  '<li class="page-item',
            'first_tag_close'   =>  '</li>',
            'prev_link'         =>  '&laquo',
            'prev_tag_open'     =>  '<li class="page-item">',
            'prev_tag_close'    =>  '</li>',
            'next_link'         =>  '&raquo',
            'next_tag_open'     =>  '<li class="page-item">',
            'next_tag_close'    =>  '</li>',
            'last_tag_open'    =>  '<li class="page-item',
            'last_tag_close'   =>  '</li>',
            'cur_tag_open'      =>  '<li class="page-item active"><a href="#" class="page-link">',
            'cur_tag_close'     =>  '<span class="sr-only">(current)</span></a></li>',
            'num_tag_open'      =>  '<li class="page-item"',
            'num_tag_close'     =>  '</li>',
        ];

        $this->pagination->initialize($config);
        return $this->pagination->create_link();
    }

}
Comment ..

Error pada saat akses modul apa? produk? coba share link

Dikomentari pada tanggal 5 June 2020 oleh Ahmad Oriza Edit  Remove 
BUAT KOMENTAR

1 JAWABAN / 2 KOMENTAR

User avatar
Ahmad Oriza menjawab pada tanggal 5 June 2020 Jadikan Jawaban Terbaik  Edit  Hapus  Ubah ke Komentar 

Ini disebabkan karena method getValidationRules tidak terbaca pada kelas. Biasanya karena salah pada saat load modelnya, atau tidak meng-extend class.

Coba lihat lagi MY_Model :

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Model extends CI_Model 
{

    protected $table    = '';
    protected $perPage  = 5;

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

        if (!$this->table) {
            $this->table = strtolower(
                str_replace('_model', '', get_class($this))
            );
        }
    }

    /**
     * Fungsi Validasi Input
     * Rules: Dideklarasikan dalam masing-masing model
     *
     * @return void
     */
    public function validate()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_error_delimiters(
            '<small class="form-text text-danger">', '</small>'
        );
        $validationRules = $this->getValidationRules();

        $this->form_validation->set_rules($validationRules);

        return $this->form_validation->run();
    }

    /**
     * Seleksi data per-kolom
     * Chain Method
     *
     * @param [type] $columns
     * @return void
     */
    public function select($columns)
    {
        $this->db->select($columns);
        return $this;
    }

    /**
     * Mencari suatu data pada kolom tertentu dengan data yang sama
     * Chain Method
     * @param [type] $column
     * @param [type] $condition
     * @return void
     */
    public function where($column, $condition)
    {
        $this->db->where($column, $condition);
        return $this;
    }

    /**
     * Mencari suatu data pada kolom tertentu dengan data yang mirip
     * Chain Method
     * @param [type] $column
     * @param [type] $condition
     * @return void
     */
    public function like($column, $condition)
    {
        $this->db->like($column, $condition);
        return $this;
    }

    /**
     * Mencari suatu data selanjutnya pada kolom tertentu dengan data yang mirip
     * Chain Method
     * @param [type] $column
     * @param [type] $condition
     * @return void
     */
    public function orLike($column, $condition)
    {
        $this->db->or_like($column, $condition);
        return $this;
    }

    /**
     * Menggabungkan Table yang berelasi yang memiliki foreign key id_namatable
     * Chain Method
     * @param [type] $table
     * @param string $type
     * @return void
     */
    public function join($table, $type = 'left')
    {
        $this->db->join($table, "$this->table.id_$table = $table.id", $type);
        return $this;
    }

    /**
     * Mengurutkan data dari hasil query dan kondisi
     * Chain Method
     * @param [type] $column
     * @param string $order
     * @return void
     */
    public function orderBy($column, $order = 'asc')
    {
        $this->db->order_by($column, $order);
        return $this;
    }

    /**
     * Menampilkan satu data dari hasil query dan kondisi
     * Hasil Akhir Chain Method
     * @return void
     */
    public function first()
    {
        return $this->db->get($this->table)->row();
    }

    /**
     * Menampilkan banyak data dari hasil query dan kondisi
     * Hasil Akhir Chain Method
     * @return void
     */
    public function get()
    {
        return $this->db->get($this->table)->result();
    }

    /**
     * Menampilkan nilai jumlah data dari hasil query dan kondisi
     * Hasil Akhir Chain Method
     * @return void
     */
    public function count()
    {
        return $this->db->count_all_results($this->table);
    }

    /**
     * Menyimpan data baru ke dalam suatu tabel
     *
     * @param [type] $data
     * @return void
     */
    public function create($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * Mengubah data yang ada pada suatu tabel dengan data baru
     *
     * @param [type] $data
     * @return void
     */
    public function update($data)
    {
        return $this->db->update($this->table, $data);
    }

    /**
     * Menghapus suatu data dari hasil query dan kondisi
     * 
     * @return void
     */
    public function delete()
    {
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }

    /**
     * Menentukan Limit data untuk ditampilkan
     *
     * @param [type] $page
     * @return void
     */
    public function paginate($page)
    {
        $this->db->limit(
            $this->perPage,
            $this->calculateRealOffset($page)
        );

        return $this;
    }

    /**
     * Menggantikan offset dengan nilai sesuai halaman
     *
     * @param [type] $page
     * @return void
     */
    public function calculateRealOffset($page)
    {
        if (is_null($page) || empty($page)) {
            $offset = 0;
        } else {
            $offset = ($page * $this->perPage) - $this->perPage;
        }

        return $offset;
    }

    /**
     * Membuat Pagination dengan style bootstrap 4
     *
     * @param [type] $baseUrl
     * @param [type] $uriSegment
     * @param [type] $totalRows
     * @return void
     */
    public function makePagination($baseUrl, $uriSegment, $totalRows = null)
    {
        $this->load->library('pagination');

        $config = [
            'base_url'          => $baseUrl,
            'uri_segment'       => $uriSegment,
            'per_page'          => $this->perPage,
            'total_rows'        => $totalRows,
            'use_page_numbers'  => true,

            'full_tag_open'     => '<ul class="pagination">',
            'full_tag_close'    => '</ul>',
            'attributes'        => ['class' => 'page-link'],
            'first_link'        => false,
            'last_link'         => false,
            'first_tag_open'    => '<li class="page-item">',
            'first_tag_close'   => '</li>',
            'prev_link'         => '&laquo',
            'prev_tag_open'     => '<li class="page-item">',
            'prev_tag_close'    => '</li>',
            'next_link'         => '&raquo',
            'next_tag_open'     => '<li class="page-item">',
            'next_tag_close'    => '</li>',
            'last_tag_open'     => '<li class="page-item">',
            'last_tag_close'    => '</li>',
            'cur_tag_open'      => '<li class="page-item active"><a href="#" class="page-link">',
            'cur_tag_close'     => '<span class="sr-only">(current)</span></a></li>',
            'num_tag_open'      => '<li class="page-item">',
            'num_tag_close'     => '</li>',
        ];

        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }
}

/* End of file MY_Model.php */

Pastikan juga pada setiap model terkait, sudah ada method getValidationRules . Contoh ini pada Product_model.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Product_model extends MY_Model 
{

    public function getDefaultValues()
    {
        return [
            'id_category'   => '',
            'slug'          => '',
            'title'         => '',
            'description'   => '',
            'price'         => '',
            'is_available'  => 1,
            'image'         => ''
        ];
    }

    public function getValidationRules()
    {
        $validationRules = [
            [
                'field' => 'id_category',
                'label' => 'Kategori',
                'rules' => 'required'
            ],
            [
                'field' => 'slug',
                'label' => 'Slug',
                'rules' => 'trim|required|callback_unique_slug'
            ],
            [
                'field' => 'title',
                'label' => 'Nama Produk',
                'rules' => 'trim|required'
            ],
            [
                'field' => 'description',
                'label' => 'Deskripsi',
                'rules' => 'trim|required'
            ],
            [
                'field' => 'price',
                'label' => 'Harga',
                'rules' => 'trim|required|numeric'
            ],
            [
                'field' => 'is_available',
                'label' => 'Ketersediaan',
                'rules' => 'required'
            ],
        ];

        return $validationRules;
    }

    public function uploadImage($fieldName, $fileName)
    {
        $config = [
            'upload_path'       => './images/product',
            'file_name'         => $fileName,
            'allowed_types'     => 'jpg|gif|png|jpeg|JPG|PNG',
            'max_size'          => 1024,
            'max_width'         => 0,
            'max_height'        => 0,
            'overwrite'         => true,
            'file_ext_tolower'  => true
        ];

        $this->load->library('upload', $config);

        if ($this->upload->do_upload($fieldName)) {
            return $this->upload->data();
        } else {
            $this->session->set_flashdata('image_error', $this->upload->display_errors('', ''));
            return false;
        }
    }

    public function deleteImage($fileName)
    {
        if (file_exists("./images/product/$fileName")) {
            unlink("./images/product/$fileName");
        }
    }

}

/* End of file Product_model.php */
BUAT KOMENTAR