コード例 #1
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $choices = range(0, 10);
     $thirdParty = $this->thirdParty;
     $existsRatings = array();
     $riskRating = null;
     $assumption = $this->assumption;
     if ($thirdParty) {
         $data = $builder->getData();
         if ($data instanceof PortfolioModel && $data->getId()) {
             $ownerId = null;
             if ($data->getParent()->getOwner()) {
                 $ownerId = $data->getParent()->getOwner();
             } else {
                 $superAdmin = $this->get('wealthbot.manager.user')->getAdmin();
                 $ownerId = $superAdmin->getId();
             }
             $modelRiskRating = $this->em->getRepository('WealthbotAdminBundle:ModelRiskRating')->findOneBy(array('owner_id' => $ownerId, 'model_id' => $data->getId()));
             if ($modelRiskRating) {
                 $riskRating = $modelRiskRating->getRating();
             }
         }
         $existsRatings = $this->getExistsRatings($riskRating);
         $modelsCount = $this->getModelsCount();
         $choices = range(0, $modelsCount);
     }
     $builder->add('name', 'text');
     if ($this->user->isSuperAdmin() || $assumption) {
         $builder->add('assumption', new ModelAssumptionFormType(), array('property_path' => false, 'data' => $assumption));
     }
     $factory = $builder->getFormFactory();
     $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($factory, $choices, $riskRating) {
         $data = $event->getData();
         $form = $event->getForm();
         if (null === $data) {
             return;
         }
         // check if the product object is not "new"
         if ($data->getId()) {
             $form->add($factory->createNamed('risk_rating', 'choice', $riskRating, array('empty_value' => 'Select Risk Rating', 'choices' => $choices)));
         }
     });
     $builder->addEventListener(FormEvents::BIND, function (FormEvent $event) use($thirdParty, $existsRatings) {
         $form = $event->getForm();
         if ($form->has('risk_rating')) {
             $riskRating = $form->get('risk_rating')->getData();
             if (in_array($riskRating, $existsRatings)) {
                 $form->get('risk_rating')->addError(new FormError('The risk with parameter :risk is already exists.', array(':risk' => $riskRating)));
             }
         }
     });
 }