validation - Symfony2 form builder without class check if multiple fields are empty -


i using form builder without class , have 2 fields each having constraints :

$form = $this->createformbuilder()         ->add('name', 'text', array(             'required'=>false,             'constraints'=> new length(array('min'=>3)         ))         ->add('dob', 'date', array(             'required'=>false,             'constraints'=> new date()         ))         ->getform()         ->handlerequest($request); 

this works great want check if both fields emtpy, , display error. can't seem handle on this. provide help???

the easiest solution set both required ...

but.. on post check simple as

if( empty($form->get('name')->getdata()) && empty($form->get('dob')->getdata())){    $form->adderror(new formerror("fill out both yo"));    // ... return view }else {    // ... persisting stuff } ...  

the symfony way possibly adding custom validator suggest @ custom validator part

pseudo :

namespace my\bundle\validator\constraints;  use symfony\component\validator\constraint; use symfony\component\validator\constraintvalidator;  class checkbothvalidator extends constraintvalidator {     public function validate($foo, constraint $constraint)     {             if (!($foo->getname() && $foo->getdob()) {                 $this->context->addviolationat('both', $constraint->message, array(), null);             }     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -