如何在 Symfony 2.4 中调试实体的验证映射? [英] How to debug an entity's validation mapping in Symfony 2.4?

查看:19
本文介绍了如何在 Symfony 2.4 中调试实体的验证映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定是否需要实体属性.

I would like to determine whether an entity property is required or not.

有人知道如何访问给定实体属性的所有约束吗?

Does anyone know how to access all of the constraints for a given entity property?

我想检查 NotBlank 约束是否对某个属性有效.

I want to check if the NotBlank constraint is active for a certain propery.

推荐答案

信息:

您可以在服务的帮助下检查类(或对象)的映射信息:

information:

You can check the mapping information for a class (or object) with the help of the service:

validator.mapping.class_metadata_factory

底层类是:

Symfony\Component\Validator\Mapping\ClassMetadataFactory

服务提供了一个方法getMetadataFor() 允许您获取类(或对象)的活动映射元数据.

The service provides a method getMetadataFor() that allows you to obtain the active mapping metadata for a class (or object).

此方法返回...的实例

This method returns an instance of...

Symfony\Component\Validator\Mapping\ClassMetadata

... 提供了一个 getPropertyMetadata(string $property) 方法,该方法返回给定属性名称的元数据.

... that provides a getPropertyMetadata(string $property) method that returns the Metadata for a given property name.

在控制器(或任何其他 ContainerAware 实例)中,您可以执行以下操作:

Inside a controller (or any other ContainerAware instance) you can do:

$factory          = $this->container->get('validator.mapping.class_metadata_factory');
$classMetadata    = $factory->getMetadataFor('Your\Bundle\Entity\Name');
$propertyMetadata = $classMetadata->getPropertyMetadata('propertyName');

这篇关于如何在 Symfony 2.4 中调试实体的验证映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆