public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $riaCompanyInformation = $this->client->getProfile()->getRia()->getRiaCompanyInformation();
     $choices = AccountGroup::getGroupChoices();
     if (!$riaCompanyInformation->getIsAllowRetirementPlan()) {
         unset($choices[AccountGroup::GROUP_EMPLOYER_RETIREMENT]);
     }
     $builder->add('groups', 'choice', array('choices' => $choices, 'multiple' => false, 'expanded' => true));
     $builder->addEventListener(FormEvents::BIND, function (FormEvent $event) use($choices) {
         $form = $event->getForm();
         $types = $form->get('groups')->getData();
         if (empty($types)) {
             $form->get('groups')->addError(new FormError('Select value.'));
         }
         if (is_array($types)) {
             foreach ($types as $type) {
                 if (!in_array($type, $choices)) {
                     $form->get('groups')->addError(new FormError('Invalid value.'));
                 }
             }
         } else {
             if (!in_array($types, $choices)) {
                 $form->get('groups')->addError(new FormError('Invalid value.'));
             }
         }
     });
 }
 public function load(ObjectManager $manager)
 {
     foreach ($this->groups as $groupName) {
         $groupObject = new AccountGroup();
         $groupObject->setName($groupName);
         $manager->persist($groupObject);
         $this->addReference('client-account-group-' . $groupName, $groupObject);
     }
     foreach ($this->types as $key => $item) {
         $typeObject = new AccountType();
         $typeObject->setName($item['name']);
         $manager->persist($typeObject);
         $this->addReference('client-account-type-' . ($key + 1), $typeObject);
         foreach ($item['groups'] as $groupKey) {
             $groupTypeObject = new AccountGroupType();
             $groupTypeObject->setGroup($this->getReference('client-account-group-' . $groupKey));
             $groupTypeObject->setType($this->getReference('client-account-type-' . ($key + 1)));
             $manager->persist($groupTypeObject);
             $this->addReference('client-account-group-type-' . $groupKey . '-' . ($key + 1), $groupTypeObject);
         }
     }
     $manager->flush();
 }
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $factory = $builder->getFormFactory();
     switch ($this->group) {
         case AccountGroup::GROUP_FINANCIAL_INSTITUTION:
             $this->buildFormForFinancialInstitution($builder);
             break;
         case AccountGroup::GROUP_DEPOSIT_MONEY:
             $this->buildFormForDepositMoney($builder);
             break;
         case AccountGroup::GROUP_OLD_EMPLOYER_RETIREMENT:
             $this->buildFormForOldEmployerRetirement($builder);
             break;
         case AccountGroup::GROUP_EMPLOYER_RETIREMENT:
             $this->buildFormForEmployerRetirement($builder);
             break;
         default:
             $this->buildFormForManually($builder);
             break;
     }
     $contributionTypes = $this->contributionTypes;
     $updateFields = function (FormInterface $form, $type, $group, $data = null) use($factory, $contributionTypes) {
         if ($type === 'contributions') {
             $form->add($factory->createNamed('monthly_contributions', 'number', null, array('grouping' => true, 'precision' => 2, 'label' => 'Estimated Monthly Contributions', 'required' => false, 'attr' => array('value' => $data))));
         } elseif ($type === 'distributions' && $group != AccountGroup::GROUP_EMPLOYER_RETIREMENT) {
             $form->add($factory->createNamed('monthly_distributions', 'number', null, array('grouping' => true, 'precision' => 2, 'label' => 'Estimated Monthly Distributions', 'required' => false, 'attr' => array('value' => $data))));
         }
         $form->remove('contribution_type');
     };
     if (in_array($this->group, AccountGroup::getGroupChoices())) {
         $group = $this->group;
         $builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) use($group, $updateFields) {
             $data = $event->getData();
             $form = $event->getForm();
             if (array_key_exists('contribution_type', $data)) {
                 $updateFields($form, $data['contribution_type'], $group);
             } else {
                 if (array_key_exists('monthly_contributions', $data)) {
                     $updateFields($form, 'contributions', $group, $data['monthly_contributions']);
                 } elseif (array_key_exists('monthly_distributions', $data)) {
                     $updateFields($form, 'distributions', $group, $data['monthly_distributions']);
                 }
             }
         });
         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($factory, $group, $updateFields, $contributionTypes) {
             $form = $event->getForm();
             $data = $event->getData();
             if ($data && $data->getId()) {
                 if ($data->getMonthlyContributions()) {
                     $type = 'contributions';
                     $value = $data->getMonthlyContributions();
                 } elseif ($data->getMonthlyDistributions()) {
                     $type = 'distributions';
                     $value = $data->getMonthlyDistributions();
                 } else {
                     $type = 'neither';
                     $value = null;
                 }
                 $updateFields($form, $type, $group, $value);
             } else {
                 if ($group === AccountGroup::GROUP_EMPLOYER_RETIREMENT) {
                     unset($contributionTypes['distributions']);
                     $contributionTypes['neither'] = 'None';
                 }
                 $form->add($factory->createNamed('contribution_type', 'choice', null, array('mapped' => false, 'choices' => $contributionTypes, 'expanded' => true, 'multiple' => false)));
             }
         });
     }
     $this->onBindProcess($builder);
 }