-
Notifications
You must be signed in to change notification settings - Fork 1
Migration from 1.x
Version 2.0 is a breaking release that fixes long-standing bugs and tightens the API. Most existing rule strings keep working unchanged; the breaking changes are about the PHP version, error behaviour and a few corrected edge cases.
- PHP 8.1 or newer is now required (1.x supported 7.4+).
In 1.x a misspelled or unknown rule was silently ignored and the field counted
as valid. In 2.0 it throws UndefinedRuleException.
$v->rule('age', 'integerr'); // 1.x: silently passes — 2.0: throwsFix any typos, and register custom names with
extend().
In 1.x a rule name that matched a global function (e.g. ctype_digit) was
invoked with the field value. This was surprising and unsafe, and has been
removed. Such names now throw. Wrap the logic in a callback or extend():
$v->extend('ctype_digit', static fn ($value): bool => ctype_digit((string) $value));Passing a callback as the rule used to throw a TypeError. It now works as
documented:
$v->rule('number', static fn ($value): bool => ((int) $value % 2) === 0,
'{field} must be even.');- The
againrule now resolves its English message (it previously fell back to the generic "not valid" text). - Message keys resolve case-insensitively, so
creditcardandcreditCardproduce the same message. - The Turkish
notContainsmessage now reads "must not contain".
If your tests asserted the old (incorrect) strings, update those expectations.
These were bugs; the new behaviour is what the documentation always implied:
- Arguments are trimmed, so
only(a, b, c)matches likeonly(a,b,c). - Open-ended
lengthworks:length(...255)now enforces the maximum. -
lengthon anullvalue returnsfalseinstead of raising a warning. -
min/maxon arrays measure the element count without a type warning. -
equalsandagaincompare loosely, soequals(123)matches the integer123. - The built-in
slugregex pattern matches multiple characters (it was missing its+quantifier).
setLocale() / setLocaleDir() now throw
LocaleException (previously
\InvalidArgumentException / \Exception), and rule() throws
InvalidArgumentException. All package
exceptions implement ExceptionInterface. If you catch the SPL types, note that
InvalidArgumentException still extends \InvalidArgumentException; the locale
errors now need the new type or the marker interface.
-
extend()for reusable custom named rules. -
getData()accessor. - An
alphanumericalias foralphanum. - Typed signatures, full PHPDoc, PHPStan (max) and PSR-12 compliance.
- Bump your PHP requirement to 8.1+.
- Search your rule strings for typos and any global-function names.
- Update tests that asserted old message text.
- Catch the new exception types where you load locales.
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other