【发布时间】:2016-03-22 16:48:09
【问题描述】:
我有一个带有注释的类用于验证。
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serialize;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Annotation\Link;
/**
* @Serialize\ExclusionPolicy("all")
* @Serialize\AccessType(type="public_method")
* @Serialize\AccessorOrder("custom", custom = {"id", "name", "awardType", "nominations"})
* @ORM\Entity(repositoryClass="AppBundle\Repository\AwardRepository")
* @ORM\Table(name="awards")
* @Link("self", route = "api_awards_show", params = { "id": "object.getId()" })
*/
class Award extends Entity
{
/**
* @Serialize\Expose()
* @Serialize\Type(name="string")
* @Assert\Type(type="string")
* @Assert\NotBlank(message="Please enter a name for the Award")
* @Assert\Length(min="3", max="255")
* @ORM\Column(type="string")
*/
private $name;
/**
* @Serialize\Expose()
* @Serialize\Type(name="AppBundle\Entity\AwardType")
* @Serialize\MaxDepth(depth=2)
* @Assert\Valid()
* @ORM\ManyToOne(
* targetEntity="AppBundle\Entity\AwardType",
* inversedBy="awards"
* )
*/
private $awardType;
/**
* @Serialize\Expose()
* @Serialize\Type(name="ArrayCollection<AppBundle\Entity\Nomination>")
* @Serialize\MaxDepth(depth=2)
* @ORM\OneToMany(
* targetEntity="AppBundle\Entity\Nomination",
* mappedBy="award"
* )
*/
private $nominations;
}
然后我使用以下内容验证该实体:
$validator = $this->get('validator');
$errors = $validator->validate($entity);
if (count($errors) > 0) {
$apiProblem = new ApiProblem(
400,
ApiProblem::TYPE_VALIDATION_ERROR
);
$apiProblem->set('errors', ['testing', 'array']);
throw new ApiProblemException($apiProblem);
}
$this->save($entity);
这很好用,问题是我无法获取有关哪些字段有错误及其错误消息的信息。在这种情况下,$errors 似乎属于未知类型,我似乎无法获取任何字段的错误消息。
我如何获得该对象的错误信息?
【问题讨论】:
标签: validation symfony