What is the Validation class in CodeIgniter 4?

Why Interviewers Ask This

This is a classic screening question for CodeIgniter roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

Answer

CodeIgniter 4's Validation service validates data from any source. Get the service: $validation = service("validation"). Method 1 — validate request directly: if (!$this->validate(["email" => "required|valid_email"])). Method 2 — validate any array: $validation->run(["email" => $email], "userRules") using rules defined in app/Config/Validation.php. Get errors: $this->validator->getErrors() or single error: $this->validator->getError("email"). In views: <?= validation_show_error("email") ?>. Custom rules: create a class with the rule as a method and register in Config/Validation.php under $ruleSets. Rule groups in config allow reusing the same rules in multiple controllers. CI4's validation integrates directly with models via $validationRules.

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex CodeIgniter answers easy to follow.