memperbaiki ErrorException Undefined property User $form_validation

error yang aku temuin seperti ini saat menjalankan function edit :

Screenshot (31).png

aku pengen nambahin library form validation tapi aku bingung menempatinnya dimana kalo di codeigniter 4. ada yang bisa membantu ?

autoload.php

<?php

namespace Config;

use CodeIgniter\Config\AutoloadConfig;

/**
 * -------------------------------------------------------------------
 * AUTO-LOADER
 * -------------------------------------------------------------------
 *
 * This file defines the namespaces and class maps so the Autoloader
 * can find the files as needed.
 *
 * NOTE: If you use an identical key in $psr4 or $classmap, then
 * the values in this file will overwrite the framework's values.
 */
class Autoload extends AutoloadConfig
{
    /**
     * -------------------------------------------------------------------
     * Namespaces
     * -------------------------------------------------------------------
     * This maps the locations of any namespaces in your application to
     * their location on the file system. These are used by the autoloader
     * to locate files the first time they have been instantiated.
     *
     * The '/app' and '/system' directories are already mapped for you.
     * you may change the name of the 'App' namespace if you wish,
     * but this should be done prior to creating any namespaced classes,
     * else you will need to modify all of those classes for this to work.
     *
     * Prototype:
     *
     *   $psr4 = [
     *       'CodeIgniter' => SYSTEMPATH,
     *       'App'         => APPPATH
     *   ];
     *
     * @var array<string, string>
     */
    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',

    ];

    /**
     * -------------------------------------------------------------------
     * Class Map
     * -------------------------------------------------------------------
     * The class map provides a map of class names and their exact
     * location on the drive. Classes loaded in this manner will have
     * slightly faster performance because they will not have to be
     * searched for within one or more directories as they would if they
     * were being autoloaded through a namespace.
     *
     * Prototype:
     *
     *   $classmap = [
     *       'MyClass'   => '/path/to/class/file.php'
     *   ];
     *
     * @var array<string, string>
     */
    public $classmap = [];
}

user.php

<?php

namespace App\Controllers;

class User extends BaseController
{

    protected $db, $builder;

    public function __construct()
    {

        $this->db     = \Config\Database::connect();
        $this->builder = $this->db->table('users');
   }

    public function index()
    {
        $data['title'] = 'My Profile';
        return view('user/index', $data);
    }

    public function edit()
    {

        $data['title'] = 'Edit Profile';
        $db     = \Config\Database::connect();
        $query =   $this->builder->get();
        $data['user'] = $query->getRow();

        $this->form_validation->set_rules('name', 'Fullname', 'required|trim');

        if ($this->form_validation->run() == false); {
            $this->load->view('user/edit', $data);
        }

        return view('user/edit', $data);
    }
}

home.php

<?php

namespace App\Controllers;

class Home extends BaseController
{

    public function __construct()
    {
        helper('form');
        $this->form_validation = \Config\Services::validation();
    }

    public function index()
    {
        return view('auth/login');
    }

    public function register()
    {
        return view('auth/register');
    }

    public function user()
    {
        return view('user/index');
    }
}

avatar kartikapatandianan
@kartikapatandianan

7 Kontribusi 1 Poin

Dipost 3 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban