2024-05-06 20:40:05 +00:00
|
|
|
---
|
2024-10-30 17:34:11 +00:00
|
|
|
date: 2020-11-13
|
2024-05-06 20:40:05 +00:00
|
|
|
id: 942ca5cd-5ca6-4ec9-9dd6-652a0b791245
|
|
|
|
title: Type Constraint
|
|
|
|
---
|
|
|
|
|
|
|
|
# Description
|
|
|
|
|
|
|
|
Validates that a given value is of a specific type. This type can be any
|
|
|
|
of the valid PHP types[^1], any of the PHP ctype functions[^2] (e.g.
|
|
|
|
alnum, alpha, digit, etc.) and also the FQCN of any class
|
|
|
|
|
|
|
|
# Syntax
|
|
|
|
|
|
|
|
``` php
|
|
|
|
// src/Entity/Author.php
|
|
|
|
namespace App\Entity;
|
|
|
|
|
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
|
|
|
|
|
|
class Author
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Assert\Type("Ramsey\Uuid\UuidInterface")
|
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Assert\Type("string")
|
|
|
|
*/
|
|
|
|
protected $firstName;
|
|
|
|
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# Changelog
|
|
|
|
|
|
|
|
- [Type Constraint Can Be An Array of
|
|
|
|
Types](20201113172816-type_constraint_can_be_an_array_of_types)
|
|
|
|
|
|
|
|
# Footnotes
|
|
|
|
|
|
|
|
[^1]: <https://www.php.net/manual/en/language.types.intro.php>
|
|
|
|
|
|
|
|
[^2]: <https://php.net/ref.ctype>
|