mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 04:56:23 +00:00
1.2 KiB
1.2 KiB
date | id | title |
---|---|---|
2020-11-17 | b632a78b-9e93-4b56-8cfe-daa3e4db12a2 | Define Constraints as Attributes |
Syntax
Annotations
// src/Entity/Author.php
namespace App\Entity;
// ...
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* @Assert\Choice(
* choices = { "fiction", "non-fiction" },
* message = "Choose a valid genre."
* )
*/
private $genre;
// ...
}
Attributes
// src/Entity/Author.php
namespace App\Entity;
// ...
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
#[Assert\Choice(
choices: ['fiction', 'non-fiction'],
message: 'Choose a valid genre.',
)]
private $genre;
// ...
}
Caveats
The following composite constraints can't be used with attributes:
- All
- AtLeastOneOf Constraint
- Collection
- Compound Constraint (abstract)
- Existance (abstract)
- Required
- Optional
- Existance (abstract)
- Sequentially Constraint
The reason is that they would require nested attributes and PHP doesn’t support that feature yet