0

我设法将验证器服务注入我的。

现在我不知道如何从实体元数据中获取不同的约束。

4

1 回答 1

2
// In your controller, get the validator:
$validator = $this->get('validator');

// Get Metadata for Class. You can use 'App\Bundle\Entity\YourEntity' as well
$meta = $validator->getMetadataFor(YourEntity::class);

// Used 'Default' as default Validation Group.
$constraints = $meta->findConstraints('Default');

http://symfony.com/doc/current/book/validation.html#using-the-validator-service

在 Symfony 2.8 上测试。对于其他版本,您可能需要稍微不同的方法。如果这对您不起作用,请使用您正在使用的 Symfony 版本更新您的问题。

于 2016-05-04T11:39:13.187 回答