Search code examples
model-view-controllermodelcontrollercodeigniter-4

Using validationRules in Models to validate form Codeigniter 4


So I have these variables $skipValidation (set specifically to FALSE), $validationRules, $validationMessages set according to this documentation

but for the life of me I can't figure out what trigger this $validationRules to run, I just assume that $skipValidation meant Codeigniter-4 already got me covered (automatically validates input before doing any queries)..

I even put $UserModel->errors() in case the validation rules catch an error

if($userModel->insert($data) === false) {
  return view('form', ['validation' => $userModel->errors()])
} else {
  redirect()->to('home');
}

I have these rules required and min_length[] applied to $validationRules but the model just skips the validationRules and insert it immediately to database rendering $validationRules useless..

Any ideas how to get validationRules in Models working? or how is it supposed to be used? I keep looping in the documentation because I don't know any better.


Solution

  • Dumb me, i was trying to figure out what is wrong with the code this whole time when its just a simple problem.. i have two models with the same filename (backup project) and i blindly edits model file that is inside the backup project..

    Perhaps for future readers seeking an answer, don't forget to check your folder path for your model file, you might make the same mistake as i did.