validators.rst 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. .. _customize.validators:
  2. ========================
  3. Using a Custom Validator
  4. ========================
  5. By default, ramsey/uuid validates UUID strings with the lenient validator
  6. :php:class:`Ramsey\\Uuid\\Validator\\GenericValidator`. This validator ensures
  7. the string is 36 characters, has the dashes in the correct places, and uses only
  8. hexadecimal values. It does not ensure the string is of the RFC 4122 variant or
  9. contains a valid version.
  10. The validator :php:class:`Ramsey\\Uuid\\Rfc4122\\Validator` validates UUID
  11. strings to ensure they match the RFC 4122 variant and contain a valid version.
  12. Since it is not enabled by default, you will need to configure ramsey/uuid to
  13. use it, if you want stricter validation.
  14. .. code-block:: php
  15. :caption: Set an alternate validator to use for Uuid::isValid()
  16. :name: customize.validators-example
  17. use Ramsey\Uuid\Rfc4122\Validator as Rfc4122Validator;
  18. use Ramsey\Uuid\Uuid;
  19. use Ramsey\Uuid\UuidFactory;
  20. $factory = new UuidFactory();
  21. $factory->setValidator(new Rfc4122Validator());
  22. Uuid::setFactory($factory);
  23. if (!Uuid::isValid('2bfb5006-087b-9553-5082-e8f39337ad29')) {
  24. echo "This UUID is not valid!\n";
  25. }
  26. .. tip::
  27. If you want to use your own validation, create a class that implements
  28. :php:interface:`Ramsey\\Uuid\\Validator\\ValidatorInterface` and use the
  29. same method to set your validator on the factory.