#20907·yii2

Add a built-in `uuid` validator (`yii\validators\UuidValidator`)

Author: WarLikeLauxCreated May 29, 2026Updated May 29, 2026

Validator::$builtInValidators ships 22 validators, but none for UUID. UUID and ULID identifiers and uuid columns are common now (framework/db/pgsql/Schema.php already maps the Postgres uuid type), yet validating user or API input means falling back to match with a hand-written pattern. Laravel (uuid, ulid) and Symfony both have a dedicated rule.

Proposal

Add framework/validators/UuidValidator.php and register it as 'uuid' in Validator::$builtInValidators:

php
['id', 'uuid'],                 // any well-formed UUID
['id', 'uuid', 'version' => 4], // require v4

Options:

  • version (int|int[]|null) require a specific UUID version (1/3/4/5/7), null accepts any well-formed UUID.
  • allowNil (bool, default true) accept the nil UUID 00000000-0000-0000-0000-000000000000.
  • standard message, skipOnEmpty, etc. inherited from the base Validator.

The check is a hex-format match (8-4-4-4-12), so it can mirror client-side in framework/assets/yii.validation.js through clientValidateAttribute() / getClientOptions(), the same way EmailValidator and IpValidator already do.

Questions

  • UUID only, or add a sibling UlidValidator ('ulid') in the same PR?
  • Client-side validation now, or as a follow-up?
  • Preference on the version option shape (single int vs a list)?

Happy to send a PR with tests (and the JS counterpart if wanted).