Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 model does not validate or updates $updatedField
#1
Question 

This is my first time working with models in CI(4). The below model works when fetching data, and also updating an excising record. But for some reason it does not validate any input data. I can leave the fields blank or fill them with a single character, they'd still update. Also when they update the $updatedField doesn't change (stays NULL).
PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;

class 
GroupsModel extends Model
{
    protected $DBGroup 'default';
    protected $table      'auth_groups';
    protected $primaryKey 'id';

    protected $useAutoIncrement true;

    protected $returnType    'array';
    protected $useSoftDeletes true;

    protected $allowedFields = ['name''description'];

    protected $useTimestamps true;
    protected $createdField  'created_at';
    protected $updatedField  'updated_at';
    protected $deletedField  'deleted_at';

    protected $validationRules    = [
        'name'        => 'required|min_length[3]|is_unique[auth_groups.name]',
        'description'    => 'required|min_length[3]',
    ];

    protected $validationMessages = [
        'name'        => [
            'is_unique' => 'Name has to be unique',
        ],
    ];
    protected $skipValidation    false;

Looking forward to your feedback!
Reply
#2

Show the code how exactly you update the data.
Reply
#3

The relevant code in the controller:
PHP Code:
                  $groupmodel=new GroupModel();
                    $group $groupmodel->find($_GET["id"]);
                    if(is_null($group)){throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();}
                    if($_SERVER['REQUEST_METHOD'] == 'POST'){$groupmodel->update($_GET["id"],$_POST);}
                    helper('form');
                    $data = [
                        'username'  =>  user()->username,
                        'title'    =>  'Groups',
                        'group'    =>  $group,
                        'errors'    =>  $groupmodel->errors(),
                    ];
                    echo view('panel/header'$data);
                    echo view('panel/admin/group_edit'$data);
                    echo view('panel/footer'$data); 
Reply
#4

Add the created_at and other time fields to your $allowedFields[]
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

At the time of writing my earlier reply is still marked for moderator approval. But an attentive contributer at my Stackoverflow pointed me to my error:
I was calling GroupModel, but the model was called GroupsModel.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB