Symfony. 5 trucos del componente Validator

Recopilación de algunas características del componente Validator de Symfony que puede que no conozcas

Image for post
Image for post
Photo by Jon Tyson on Unsplash

Validación automática

/** @ORM\Entity */
class Person
{
// ...

/** @ORM\Column(type="string", length=5) */
public $zip;
}
$person = new Person();
$person->setZip('1111111111111111');
$violationList = $validator->validate($person);
framework:
validation:
...
auto_mapping:
App\Entity\: []

NotCompromisedPassword

App\Entity\User:
properties:
rawPassword:
- NotCompromisedPassword

Callback

App\Entity\Person:
constraints:
- Callback: validateContact
class Person
{

public function validateContact(ExecutionContextInterface $context, $payload)
{

// check if the name is actually a fake name
if (empty($this->telephone) && empty($this->email)) {
$context->buildViolation('We need at least your email or your telephone')
->atPath('email')
->addViolation();
}
}
}

UniqueEntity

Image for post
Image for post

Choice + Callback

Image for post
Image for post
Image for post
Image for post

Bola extra. User Password

Image for post
Image for post
https://symfony.com/doc/current/reference/constraints/UserPassword.html

Conclusiones

¿Quieres recibir más artículos como este?

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store