Reputation: 35
I would like to use validation inside the service.
services.yml:
AppBundle\Service\BookingCreateService:
arguments: [ "@doctrine.orm.entity_manager" , "@service_container"]
public: true
Status Entity:
/**
* @var string
* @ORM\Column(name="Status", type="string", length=35)
* @Assert\Length(min="30", minMessage="error")
*/
private $status;
BookingCreateService:
class BookingCreateService
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function create()
{
$a= new Status();
$a->setStatus("222");
$this->em->persist($a);
$this->em->flush();
}
}
I would like the message about not meeting the validation conditions to be sent from the service to the controller. And then to the twig template. I have read the documentation, but I do not know how to go about it :(
Upvotes: 0
Views: 65
Reputation: 306
I think you're wrong. Indeed, you do not need to call the validator in your service. This one is called automatically when you try to persist your entity. Just follow the symfony doc to Create a custom Validation Constraint
Upvotes: 0
Reputation: 35
I've done it for now, but it can definitely be done better
public function create()
{
$errors = $this->validator->validate($a);
if (count($errors) > 0) {
$errorsString = (object) $errors;
return $errorsString;
}
}
Upvotes: 1