wiki/content/20201117112727-define_constraints_as_attributes.md

67 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
date: 2020-11-17
id: b632a78b-9e93-4b56-8cfe-daa3e4db12a2
title: Define Constraints as Attributes
---
# Syntax
## Annotations
``` php
// 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
``` php
// 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](20201116134435-atleastoneof_constraint)
- Collection
- [Compound Constraint](20201116124210-compound_constraint) (abstract)
- Existance (abstract)
- Required
- Optional
- [Sequentially Constraint](20201116124823-sequentially_constraint)
The reason is that they would require nested attributes and PHP doesnt
support that feature yet