private function checkAddressLocation(&$address, $form, &$em)
 {
     $form = $form->get('location');
     if (!$address->getCountry()) {
         $form->get('country')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     $isPreferred = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($address->getCountry());
     if (!$address->getState() && !$isPreferred) {
         $steppe = ucwords(strtolower(trim($form->get('statesteppe')->getData())));
         if ($steppe) {
             $repo = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:State');
             $ent = $repo->findOneBy(array('name' => $steppe, 'country' => $address->getCountry()));
             if (!$ent) {
                 $ent = new State();
                 $ent->setName($steppe);
                 $ent->setCountry($address->getCountry());
                 $em->persist($ent);
             }
             $address->setState($ent);
         }
     }
     if (!$address->getState()) {
         $form->get('state')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     if (!$address->getCity() && !$isPreferred) {
         $steppe = ucwords(strtolower(trim($form->get('citysteppe')->getData())));
         if ($address->getState()) {
             $state = $address->getState();
         } elseif (isset($ent)) {
             $state = $ent;
         } else {
             $state = null;
         }
         if ($state && $steppe) {
             $repo = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:City');
             $ent = $repo->findOneBy(array('name' => $steppe, 'state' => $state));
             if (!$ent) {
                 $ent = new City();
                 $ent->setName($steppe);
                 $ent->setState($state);
                 $em->persist($ent);
             }
             $address->setCity($ent);
         }
     }
     if (!$address->getCity()) {
         $form->get('city')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     return true;
 }
 public function onProfileEditSuccess(FormEvent $event)
 {
     $user = $event->getForm()->getData();
     if (!$user->getState()) {
         $steppe = ucwords(strtolower(trim($event->getForm()->get('ufsteppe')->getData())));
         if ($steppe) {
             if ($user->getCountry()) {
                 $isPreferred = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($user->getCountry());
                 if ($isPreferred) {
                     throw new LcValidationException('restrict.location.creation');
                 }
             } else {
                 throw new LcValidationException('required.field.country');
             }
             $repo = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:State');
             $ent = $repo->findOneBy(array('name' => $steppe, 'country' => $user->getCountry()));
             if (!$ent) {
                 $ent = new State();
                 $ent->setName($steppe);
                 $ent->setCountry($user->getCountry());
                 $this->em->persist($ent);
             }
             $user->setState($ent);
         }
     }
     if (!$user->getCity()) {
         $steppe = ucwords(strtolower(trim($event->getForm()->get('citysteppe')->getData())));
         if ($steppe) {
             if ($user->getState()) {
                 $isPreferred = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($user->getState()->getCountry());
                 if ($isPreferred) {
                     throw new LcValidationException('restrict.location.creation');
                 }
             } else {
                 throw new LcValidationException('required.field.state');
             }
             $repo = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:City');
             $ent = $repo->findOneBy(array('name' => $steppe, 'state' => $user->getState()));
             if (!$ent) {
                 $ent = new City();
                 $ent->setName($steppe);
                 $ent->setState($user->getState());
                 $this->em->persist($ent);
             }
             $user->setCity($ent);
         }
     }
     $this->checkEmailChanged($user);
     // default:
     $url = $this->router->generate('fos_user_profile_edit');
     $event->setResponse(new RedirectResponse($url));
 }
 private function registerTextualState(\Symfony\Component\Form\FormEvent $event)
 {
     $data = $event->getForm()->getData();
     if (!$data instanceof SelectData) {
         return;
     }
     $form = $event->getForm();
     if ($form->has('state_text')) {
         $stateTextInput = $form->get('state_text')->getData();
         $stateText = ucwords(strtolower(trim($stateTextInput)));
         if (!$stateText) {
             return;
         }
         if (!$data->getCountry()) {
             throw new LcValidationException('required.field.country');
         }
         $isPreferred = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($data->getCountry());
         if ($isPreferred) {
             throw new LcValidationException('restrict.location.creation');
         }
         $repo = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:State');
         $state = $repo->findOneBy(array('name' => $stateText, 'country' => $data->getCountry()));
         if (!$state) {
             $state = new State();
             $state->setName($stateText);
             $state->setCountry($data->getCountry());
             $this->em->persist($state);
             $this->em->flush($state);
         }
         $data->setState($state)->setCity(null);
     }
 }