2014-11-01 80 views
2

在我的表單中我有兩個datetime字段:startDateendDatestartDate不能早於當前時間。 endDate不能早於或等於startDate,它不能超過startDate一個月。Symfony2窗體日期時間字段驗證

所以我的問題是我如何可以實現這些領域的驗證?我對Symfony相當陌生,所以如果你可以添加和舉例的話,我會非常感激。

我的形式:

<?php 
namespace Atotrukis\MainBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Doctrine\ORM\EntityRepository; 
use Symfony\Component\Validator\Constraints as Assert; 
class CreateEventFormType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('name', 'text', [ 
       'constraints' =>[ 
        new Assert\NotBlank([ 
         'message' => "Renginio pavadinimas negali būti tuščias" 
        ]), 
        new Assert\Length([ 
         'min' => "2", 
         'max' => "255", 
         'minMessage' => "Renginio pavadinimas negali būti trumpesnis nei {{ limit }} simboliai", 
         'maxMessage' => "Renginio pavadinimas negali būti ilgesnis nei {{ limit }} simboliai" 
        ]) 
       ] 
      ]) 
      ->add('description', 'textarea', [ 
       'constraints' =>[ 
        new Assert\NotBlank([ 
         'message' => "Renginio aprašymas negali būti tuščias" 
        ]), 
        new Assert\Length([ 
         'min' => "10", 
         'max' => "5000", 
         'minMessage' => "Renginio aprašymas negali būti trumpesnis nei {{ limit }} simbolių", 
         'maxMessage' => "Renginio aprašymas negali būti ilgesnis nei {{ limit }} simbolių" 
        ]) 
       ] 
      ]) 
      ->add('startDate', 'datetime') 
      ->add('endDate', 'datetime') 
      ->add('map', 'text') 
      ->add('city', 'entity', array(
       'class' => 'AtotrukisMainBundle:City', 
       'property' => 'name', 
       'constraints' =>[ 
        new Assert\NotBlank([ 
         'message' => "Pasirinkite miestą" 
        ]) 
       ], 
       'empty_value' => 'Pasirinkite miestą', 
       'query_builder' => function(EntityRepository $er) { 
        return $er->createQueryBuilder('c') 
         ->addOrderBy('c.priority', 'ASC') 
         ->addOrderBy('c.name', 'ASC'); 
       }, 
      )) 
      ->add('save', 'submit', array('label' => 'Sukurti')); 
    } 
    public function getName() 
    { 
     return 'createEventForm'; 
    } 
} 

回答

1

我能想到的,你可以acchive你問哪兩種方式。

第一種方法是創建一個類約束驗證器。你可以按照我的回答在類似的問題here和關於sf2文檔的章節here

第二種方法是利用Callback約束。例如,在你CreateEventFormType類:

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'constraints'=> 
       array(new Callback(array('methods'=>array(array($this, 'customFormValidation'))))), 
    )); 
} 

public function customFormValidation($data, ExecutionContextInterface $context) 
{ 
    if ($data['startDate'] && $data['endDate'] && $data['startDate'] > $data['endDate']) { 
     $context->addViolationAt('startDate', 'your constraint message here'); 
    } 
} 
0

SF3/Silex2更新(我知道這個問題是SF2) 這個代碼是Silex的,但唯一的區別是製造商的工廠功能和渲染的東西。

$form = $app->namedForm('form', $data, [ 
     'constraints' => array(
      new Callback(function ($data, ExecutionContextInterface $context) { 
       if ($data['to'] && $data['from'] && $data['to'] < $data['from']) { 
      $context->buildViolation('error')->addViolation(); 
       } 
      }), 
     ), 
    ]) 
     ->add('from', DateType::class) 
     ->add('to', DateType::class) 
     ->add('save', SubmitType::class) 
     ->getForm(); 

    $form->handleRequest($request); 

    if ($form->isSubmitted()) { 

     if ($form->isValid()) { 
      // your submit stuff 
      return $app->redirect($request->getUri()); 
     } 
     // error validation does not dispay errors after a redirect 
    } 

    return $app['twig']->render('form.html.twig', [ 
     'form' => $form->createView(), 
    ]);