What is the Validation class in CodeIgniter 4?

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.