Skip to content

Centum\Validator

Validators are used to validate data and provide useful error messages explaining why a piece of data failed. They provide a consistent way to enforce rules across your application, ensuring data integrity and clear feedback for developers and users.

Validators must implement Centum\Interfaces\Validator\ValidatorInterface.

Validators require one public method:

The validate() method must return an array of string messages explaining why the data is not valid. If a piece of data is valid, then validate() would return an empty array. This makes the interface simple to use: check if the result is empty, and if not, you have descriptive error messages ready for display or logging.

You can create custom validators to encapsulate specific business rules. For example, you might validate that a string contains only alphanumeric characters, or that a number is within a certain range. By splitting responsibilities into small validator classes, your code becomes easier to test, maintain, and reuse.