Home > Software design >  how to display validation errors inside the controller of codeigniter?
how to display validation errors inside the controller of codeigniter?

Time:08-05

I have basic codeigniter mvc structure where my form data comes into controller and checks for the validations. i am writing an API to give back the data or to display the errors. what function should i use to get the validation errors in the controller?

CodePudding user response:

Check this validation documentation https://codeigniter4.github.io/userguide/libraries/validation.html

Use CodeIgniter 4 ResponseTrait to respond with JSON.

use CodeIgniter\API\ResponseTrait;

class YourClass extends BaseController
{
    use ResponseTrait;

    public function yourFunction() {
        return $this->respond($data, 200);
    }
    
}

CodePudding user response:

$this->validator->getErrors();

This line will give you errors in the JSON format.

Example:

{ "mobile_no": "The mobile_no field is required." }

  • Related