Get validator constraints of an entity

你离开我真会死。 提交于 2019-12-12 03:28:46

问题


I managed to inject the validator service into mine.

Now I can't figure out how to get the different constraints from an entity metadata .


回答1:


// 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

Tested on Symfony 2.8. For other versions you might need a slightly different approach. If this is not working for you, please update your question with the Symfony version you are using.



来源:https://stackoverflow.com/questions/37025778/get-validator-constraints-of-an-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!