Ejemplo n.º 1
0
 public function onBind(FormEvent $event)
 {
     $form = $event->getForm();
     $data = $event->getData();
     $cleanedPhoneNumber = str_replace(array(' ', '-', '(', ')'), '', $data->getPhoneNumber());
     $data->setPhoneNumber($cleanedPhoneNumber);
     $bankInformationValidator = new BankInformationFormValidator($form, $data);
     $bankInformationValidator->validate();
 }
 protected function validateFields(FormInterface $form, $data)
 {
     if (!in_array($data->getType(), OneTimeContribution::getTypeChoices())) {
         $form->get('type')->addError(new FormError('Choose an option.'));
     } else {
         if ($data->getType() == OneTimeContribution::TYPE_FUNDING_BANK) {
             $bankInformationValidator = new BankInformationFormValidator($form->get('bankInformation'), $data->getBankInformation());
             $bankInformationValidator->validate();
             if (!$data->getStartTransferDate() instanceof \DateTime) {
                 $form->get('start_transfer_date_month')->addError(new FormError('Enter correct date.'));
             } else {
                 $minDate = new \DateTime('+5 days');
                 if ($data->getStartTransferDate() < $minDate) {
                     $form->get('start_transfer_date_month')->addError(new FormError("The start of your transfer should be at least 5 days after today’s date."));
                 }
             }
             if (!$data->getAmount()) {
                 $form->get('amount')->addError(new FormError('Required.'));
             }
             if ($form->has('transaction_frequency')) {
                 $frequency = $form->get('transaction_frequency')->getData();
                 if (null === $frequency || !is_numeric($frequency)) {
                     $form->get('transaction_frequency')->addError(new FormError('Choose an option.'));
                 }
             }
             if ($form->has('contribution_year')) {
                 $contributionYear = $data->getContributionYear();
                 if (null === $contributionYear || !is_numeric($contributionYear)) {
                     $form->get('contribution_year')->addError(new FormError('Enter valid year.'));
                 } else {
                     $currDate = new \DateTime();
                     $currYear = $currDate->format('Y');
                     $minDate = new \DateTime($currYear . '-01-01');
                     $maxDate = new \DateTime($currYear . '-04-15');
                     $startTransferDate = $data->getStartTransferDate();
                     if ($startTransferDate < $minDate || $startTransferDate > $maxDate) {
                         if ($contributionYear !== $currYear) {
                             $form->get('contribution_year')->addError(new FormError(sprintf('Value should be equal %s', $currYear)));
                         }
                     } else {
                         $prevYear = $currDate->add(\DateInterval::createFromDateString('-1 year'))->format('Y');
                         if ($contributionYear !== $currYear && $contributionYear !== $prevYear) {
                             $form->get('contribution_year')->addError(new FormError(sprintf('Value should be equal %s or %s', $prevYear, $currYear)));
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * BIND event handler
  * Validate form fields
  *
  * @param FormEvent $event
  */
 public function bind(FormEvent $event)
 {
     $data = $event->getData();
     $form = $event->getForm();
     if ($form->has('transfer_date_month') && $form->has('transfer_date_day')) {
         $month = $form->get('transfer_date_month')->getData();
         $day = $form->get('transfer_date_day')->getData();
         $year = date('Y');
         if ($month && $day) {
             $date = new \DateTime($year . '-' . $month . '-' . $day);
             $data->setTransferDate($date);
         }
     }
     $validator = new ScheduledDistributionFormValidator($form, $data);
     $validator->validate();
     if ($form->has('bankInformation')) {
         if (!$data->getBankInformation()) {
             $form->get('bankInformation')->addError(new FormError('Required.'));
         } else {
             $bankInformationValidator = new BankInformationFormValidator($form->get('bankInformation'), $data->getBankInformation());
             $bankInformationValidator->validate();
         }
     }
 }