php - Limiting the number of radio buttons checked by a user after submit in Symfony2 form -
i new symfony2. have created form has 3 sets of radio buttons , submit button. display form, have used formtype. now, have apply condition user can select maximum of 2 sets of radio buttons(and minimum of 0 sets of radio buttons) , not 3 sets of radio buttons. if user selects 3 sets of radio buttons , clicks on submit want throw error message user saying "you can select 2".
this formtype "subscriptionstype.php"
<?php namespace instituteevents\studentbundle\form\type; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; class subscriptionstype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('event1', 'choice', array('choices' => array('1' => 'tourism', '2' => 'food party', '3' => 'south korean food', '4' => 'cooking', '5' => 'none of above'), 'data' => '5', 'expanded' => true, 'multiple' => false)) ->add('event2', 'choice', array('choices' => array('6' => 'cricket', '7' => 'football', '8' => 'hockey', '9' => 'baseball', '10' => 'polo', '5' => 'none of above'), 'data' => '5', 'expanded' => true, 'multiple' => false)) ->add('event3', 'choice', array('choices' => array('11' => 'game 1', '12' => 'game 2', '13' => 'game 3', '14' => 'game 4', '15' => 'game 5', '5' => 'none of above'), 'data' => '5', 'expanded' => true, 'multiple' => false)) ->add('register', 'submit'); } public function getname() { return 'subscriptions'; } }
this controller "defaultcontroller"
<?php namespace instituteprojectevents\studentbundle\controller; use symfony\bundle\frameworkbundle\controller\controller; use instituteprojectevents\studentbundle\entity\subscriptions; use instituteprojectevents\studentbundle\form\type\subscriptionstype; use symfony\component\httpfoundation\request; class defaultcontroller extends controller { public function eventsoneaction(request $request) { $subscriptions = new subscriptions(); //get logged in users id $user = $this->get('security.context')->gettoken()->getuser(); $userid = $user->getid(); //check if events selected user $repository = $this->getdoctrine()->getmanager() ->getrepository('instituteprojecteventsstudentbundle:subscriptions'); $query = $repository->findoneby(array('id' => $userid)); if(($query == null)) { $subscriptions->setevent1(5); $subscriptions->setevent2(5); $subscriptions->setevent3(5); $subscriptions->setstudents($userid); $form = $this->createform(new subscriptionstype(), $subscriptions); $form->handlerequest($request); } else { $subscriptions->setevent1($query->getevent1()); $subscriptions->setevent2($query->getevent2()); $subscriptions->setevent3($query->getevent3()); $subscriptions->setstudents($userid); $form = $this->createform(new subscriptionstype(), $subscriptions); $form->handlerequest($request); } if ($form->isvalid()) { //save database $em = $this->getdoctrine()->getmanager(); $em->persist($subscriptions); $em->flush(); return $this->redirect($this->generateurl('instituteproject_events_student_eventsregistered')); } if($current_date > date_format($date,"y/m/d h:i:s a")) { return $this->render('instituteprojecteventsstudentbundle:default:registrationsclosed.html.twig'); } else { $form = $this->createform(new subscriptionstype(), $subscriptions); return $this->render('instituteprojecteventsstudentbundle:default:eventsday1.html.twig', array('form' => $form ->createview())); } } /** * action displays confirmation page on success. */ public function eventsregisteredaction() { return $this->render('instituteprojecteventsstudentbundle:default:eventsregistered.html.twig'); } }
what validation needs written in "defaultcontroller.php" apply rule of selection of maximum of 2 sets of radio buttons , minimum of 0?
what applying callback
constraint whole form fields?
... public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver ->setdefaults(array( ... 'constraints' => array( new callback( array('callback' => array($this, 'validateform')) ) ) )); } public function validateform($data, executioncontextinterface $context) { if (condition) { // build , add violation } }
alternatively can check request
object..
Comments
Post a Comment