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
Post a Comment