Reputation: 1332
I have the following Entity with Assert Validation:
/**
* 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;
}
I use this validation with form-binding. But now, I just want to validate this entity without a form-binding like this:
$appointment = new Appointment();
$validator = $this->get('validator');
$errors = $validator->validate($appointment);
But I always retrieve zero errors? Also when I leave lastname blank. Do I miss something?
Thanks in advance
Upvotes: 1
Views: 783
Reputation: 2167
Lastname will only be checked against the NotBlank when you use step2 validation group.
Try this:
$errors = $validator->validate($appointment, array('step2'));
Upvotes: 3