What is the Form Validation in CodeIgniter 4?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for CodeIgniter development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
CodeIgniter 4 provides a flexible Validation class. Get the service: $validation = \Config\Services::validation(). Set rules: $validation->setRules(["username" => "required|min_length[5]|max_length[50]", "email" => "required|valid_email"]). Run validation: if (!$validation->withRequest($this->request)->run()) { return redirect()->back()->withInput()->with("errors", $validation->getErrors()); }. Built-in rules: required, min_length[n], max_length[n], exact_length[n], valid_email, valid_url, alpha, alpha_numeric, numeric, integer, is_unique[table.field], differs[field]. Custom error messages: pass as third parameter to setRules(). In CI4 models, define $validationRules property for automatic validation on insert/update.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last CodeIgniter project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What is the Session library in CodeIgniter 4?
Next
What is the Database class in CodeIgniter 4?