-
Notifications
You must be signed in to change notification settings - Fork 1
Exceptions
Every exception the package throws lives under InitPHP\Validation\Exception and
implements the marker interface ExceptionInterface, so you can catch them all
at once.
Throwable
└── ExceptionInterface (interface)
├── ValidationException (extends \RuntimeException)
│ ├── UndefinedRuleException
│ └── LocaleException
└── InvalidArgumentException (extends \InvalidArgumentException)
A failed validation is not an exception — it is the normal
falsereturn ofvalidation(), with messages ingetError(). Exceptions are reserved for configuration and programming errors.
Thrown by validation() when a string rule names a rule that is neither built-in
nor registered with extend(). Unknown rules fail
loudly rather than silently passing — a misspelled rule is a bug, not a valid
field.
use InitPHP\Validation\Exception\UndefinedRuleException;
$v->rule('age', 'integerr'); // typo
try {
$v->validation();
} catch (UndefinedRuleException $e) {
echo $e->getMessage(); // 'The validation rule "integerr" is not defined.'
}Thrown by rule() when a key or rule entry has an unsupported type — for example
an array rule entry that is neither a string nor a callable. It extends the SPL
\InvalidArgumentException and also implements ExceptionInterface.
use InitPHP\Validation\Exception\InvalidArgumentException;
try {
$v->rule('age', [123]); // 123 is not a string or callable
} catch (InvalidArgumentException $e) {
// ...
}Thrown by setLocale() and setLocaleDir() when a locale cannot be loaded: the
directory does not exist, the file is missing, or the file does not return an
array.
use InitPHP\Validation\Exception\LocaleException;
try {
$v->setLocale('xx');
} catch (LocaleException $e) {
// could not find the language file
}use InitPHP\Validation\Exception\ExceptionInterface;
try {
$v->rule('age', 'integer')->validation();
} catch (ExceptionInterface $e) {
// any failure originating from InitPHP Validation
}- API Reference — which method throws what.
- Troubleshooting — common causes and fixes.
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other