0

我想在服务中使用验证。

服务.yml:

AppBundle\Service\BookingCreateService:
            arguments: [ "@doctrine.orm.entity_manager" , "@service_container"]
            public: true

状态实体:

/**
 * @var string
 * @ORM\Column(name="Status", type="string", length=35)
 * @Assert\Length(min="30", minMessage="error")
 */
private $status;

预订创建服务:

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();

    }
}

我希望将有关不满足验证条件的消息从服务发送到控制器。然后到树枝模板。我已经阅读了文档,但我不知道如何去做:(

4

2 回答 2

1

我现在已经做到了,但肯定可以做得更好

public function create()
    {

        $errors = $this->validator->validate($a);

        if (count($errors) > 0) {

            $errorsString = (object) $errors;

            return $errorsString;
        }

    }
于 2018-01-11T01:58:13.387 回答
0

我认为你错了。事实上,您不需要在您的服务中调用验证器。当您尝试持久化实体时,会自动调用此方法。只需按照 symfony 文档创建自定义验证约束

于 2018-02-09T10:08:39.943 回答