Symfony 2.3:无形式验证实体

时间:2014-05-07 09:11:01

标签: php validation symfony entity

我有以下带有断言验证的实体:

/**
* Appointment
*
* @ORM\Table(name="appointment")
* @ORM\Entity(repositoryClass="AppointmentBundle\Entity\AppointmentRepository")
* @ORM\HasLifecycleCallbacks
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
* @Assert\Callback(methods={"customValidation"}, groups={"step2"})
*/
class Appointment {
   /**
    * @var \DateTime
    * @Assert\NotNull(message="form.appointment.requestdate", groups={"step1"})
    * @ORM\Column(name="request_date", type="date")
    * @JMS\Type("DateTime<'Y-m-d'>")
    */
   private $requestDate;

   /**
    * @var string
    *
    * @ORM\Column(name="delivery_time", type="string", length=5)
    * @Assert\Length(max=5)
    */
    private $deliveryTime;

    ...

    /**
     * @var string
     * @Assert\NotBlank(groups={"step2"})
     * @ORM\Column(name="lastname", type="string", length=64)
     * @Assert\Length(groups={"step2"}, max=64)
     */
     private $lastname;
}

我将此验证用于表单绑定。但是现在,我只想验证这个实体没有像这样的表单绑定:

$appointment = new Appointment();

$validator = $this->get('validator');
$errors = $validator->validate($appointment);

但我总是检索零错误?当我把lastname留空时。 我想念一下吗?

提前致谢

1 个答案:

答案 0 :(得分:3)

使用step2验证组时,只会根据NotBlank检查姓氏。

试试这个:

$errors = $validator->validate($appointment, array('step2'));