我有一个包含多个日期字段的表单。我需要检查一下eventStop
< eventStart
(日期字段)。如果它是真的,$form->isValid()
应该停止脚本并在页面上放置错误。
所以根据
http://symfony.com/doc/current/cookbook/validation/custom_constraint.html#constraint-validators-with-dependencies
我已经创建了新的自定义验证约束,但是当我提交表单时,我的验证不会执行,脚本会跳过它...
我的代码: MyBundle \验证\约束\ EventFormFieldsValidator.php
namespace MyBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Doctrine\ORM\EntityManager;
/**
* @Annotation
*/
class EventFormFieldsValidator extends ConstraintValidator {
private $em;
public function __construct(EntityManager $em) {
$this->em = $em;
}
public function validate($object, Constraint $constraint) {
die('end'); /* just for check */
$this->context->addViolationAt('eventStart', 'There is already an event during this time!');
$this->context->addViolation('There is already an event during this time!');
}
}
MyBundle \验证\约束\ EventFormFields.php
namespace MyBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class EventFormFields extends Constraint {
public function validatedBy() {
return 'event_form_validator';
}
public function getTargets() {
return self::CLASS_CONSTRAINT;
}
}
MyBundle \实体\ Event.php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use MyBundle\Validator\Constraints as ValidateAssert;
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="MyBundle\Entity\EventRepository")
* @ORM\Table(name="events")
* @ValidateAssert\EventFormFields
*
*/
class Event {
....
MyBundle \资源\配置\ services.yml
services:
validator.unique.my_form_validator:
class: MyBundle\Validator\Constraints\EventFormFieldValidator
arguments:
- "@doctrine.orm.entity_manager"
tags:
- { name: validator.constraint_validator, alias: event_form_validator }
应用\资源\配置\ services.yml
imports:
- { resource: "@MyBundle/Resources/config/services.yml" }
MyBundle \形式\ EventFormType.php
namespace MyBundle\Forms;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;
class EventFormType extends AbstractType {
public function getName() {
return 'c_event';
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('eventName', 'text', array(
'label' => 'Event name',
'attr'=> array('class'=>'input-sm')
))
->add('location', 'text', array(
'label' => 'Location',
'attr'=> array('class'=>'input-sm')
))
->add('description', 'textarea', array(
'label' => 'Description',
'attr'=> array('class'=>'input-sm')
))
->add('eventStart','collot_datetime', array(
'label' => 'Event start',
'attr'=> array('class'=>'input-sm'),
'pickerOptions' => [
'format' => 'yyyy-mm-dd hh:ii:00',
'weekStart' => 1,
'autoclose' => true,
'keyboardNavigation' => true,
'language' => 'en',
'minuteStep' => 5,
'pickerReferer ' => 'default', //deprecated
'pickerPosition' => 'bottom-right',
]
))
->add('eventStop','collot_datetime', array(
'label' => 'Event end',
'attr'=> array('class'=>'input-sm'),
'pickerOptions' => [
'format' => 'yyyy-mm-dd hh:ii:00',
'weekStart' => 1,
'autoclose' => true,
'keyboardNavigation' => true,
'language' => 'en',
'minuteStep' => 5,
'pickerReferer ' => 'default', //deprecated
'pickerPosition' => 'bottom-right',
]
))
->add('eventSignUpEndDate','collot_datetime', array(
'label' => 'Sign up until',
'attr'=> array('class'=>'input-sm'),
'pickerOptions' => [
'format' => 'yyyy-mm-dd hh:ii:00',
'weekStart' => 1,
'autoclose' => true,
'keyboardNavigation' => true,
'language' => 'en',
'minuteStep' => 5,
'pickerReferer ' => 'default', //deprecated
'pickerPosition' => 'bottom-right',
]
))
->add('maxGuests','integer', array(
'label' => 'Max guests',
'required' => false,
'attr'=> array(
'placeholder' => 'Empty for unlimited',
'class' => 'input-sm'
)
))
->add('latitude','text', array(
'label' => 'Latitude',
'attr'=> array(
'class'=>'input-sm',
)
))
->add('longitude','text', array(
'label' => 'Longitude',
'attr'=> array(
'class'=>'input-sm',
)
))
->add('private','checkbox', array(
'label' => 'Private',
'required' => false,
));
}
public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Event',
'validation_groups' => array('registration'),
));
}
}
答案 0 :(得分:0)
您需要在验证定义中添加验证组,如下所示:
MyBundle \实体\ Event.php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use MyBundle\Validator\Constraints as ValidateAssert;
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="MyBundle\Entity\EventRepository")
* @ORM\Table(name="events")
* @ValidateAssert\EventFormFields(groups={"registration"})
*
*/
class Event {
希望这个帮助