What is CodeIgniter 4's Model Validation?
Why Interviewers Ask This
Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.
Answer
CI4 models can validate data automatically before insert/update. Define rules in the model: protected $validationRules = ["name" => "required|min_length[3]", "email" => "required|valid_email|is_unique[users.email]"]. Custom messages: protected $validationMessages = ["email" => ["is_unique" => "This email is already registered"]]. Skip validation for specific operations: $this->userModel->skipValidation(true)->save($data). Call validation manually: if (!$this->userModel->validate($data)) { $errors = $this->userModel->errors(); }. The save() method runs validation automatically — it returns false on failure. For update, the is_unique rule needs to ignore the current record: use placeholders: "is_unique[users.email,id,{id}]" where {id} is a placeholder replaced with the actual value at runtime. This keeps validation logic close to the model.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a CodeIgniter codebase.
Previous
What is CodeIgniter 4's Query Builder?
Next
What is the Database Forge class in CodeIgniter 4?