Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.57 KB | None | 0 0
  1.    
  2.     //AppBundle\Controller\AdminController.php
  3.  
  4.     /.../
  5.     /**
  6.      *
  7.      * @Route("/admin/register-doctor", name="doctor-registration")
  8.      */
  9.     public function registerAction(Request $request)
  10.     {
  11.         /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
  12.         $userManager = $this->get('fos_user.user_manager');
  13.  
  14.  
  15.         $user = $userManager->createUser();
  16.  
  17.         $personal = new PersonPersonal();
  18.         $info = new PersonMainInfo();
  19.         $personal->setPersonMainInfo($info);
  20.         $personal->setPersonal($user);
  21.  
  22.         $form = $this->createForm(PersonPersonalType::class, $personal,array(
  23.             'action' => $this->generateUrl('doctor-registration'),
  24.             'method' => 'POST'));
  25.  
  26.  
  27.             $form->handleRequest($request);
  28.  
  29.             if ($form->isSubmitted() && $form->isValid()) {
  30.                 dump($form->getData());dump($form->getErrors());die;
  31.  
  32.  
  33.                 $form->getData();
  34.                 $user->setPlainPassword("Default");
  35.                 $userManager->updateUser($user);
  36.  
  37.                 $em = $this->getDoctrine()->getEntityManager();
  38.  
  39.                 $personal->setCreationDate(new \DateTime());
  40.                 $em->persist($info);
  41.                 $em->flush();
  42.                 $em->persist($personal);
  43.                 $em->flush();
  44.  
  45.                 $request->getSession()->getFlashBag()->add(
  46.                     'success',
  47.                     'Personalo darbuotojas sėkmingai pridėtas'
  48.                 );
  49.                 return $this->redirectToRoute('admin');
  50.             } else {
  51.                 $errors = $form->getErrors();
  52.                 if (!empty($errors)) {
  53.                     foreach ($errors as $error) {
  54.                         $request->getSession()->getFlashBag()->add(
  55.                             'error',
  56.                             $error->getMessage()
  57.                         );
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             return $this->render('main/register-doctor.html.twig', array(
  63.                 'form' => $form->createView(),
  64.             ));
  65.         /.../
  66.         }
  67.  
  68.     //AppBundle\Entity\PersonMainInfo.php
  69.  
  70.     /.../
  71.        
  72.     /**
  73.      * @var string
  74.      * @Assert\NotBlank()
  75.      * @ORM\Column(name="address", type="string", length=500, nullable=true)
  76.      */
  77.      private $address;
  78.     /**
  79.      * @var string
  80.      * @Assert\Email(
  81.      *     message = "The email '{{ value }}' is not a valid email.",
  82.      *     checkMX = true
  83.      * )
  84.      * @ORM\Column(name="email", type="string", length=600, nullable=true)
  85.      */
  86.     private $email;
  87.     /.../
  88.  
  89.     //AppBundle\Form\Type\PersonMainInfoType.php
  90.  
  91.     /.../
  92.  
  93.     class PersonMainInfoType extends AbstractType
  94.     {
  95.          public function buildForm(FormBuilderInterface $builder, array $options)
  96.          {
  97.              // Validations only in Entity class with annotations? or need to validate here to??
  98.               $builder
  99.                      ->add('name')
  100.                      ->add('lastname')
  101.                      ->add('birth_date', DateType::class, array(
  102.                          'placeholder' => array(
  103.                              'year' => 'Year', 'month' => 'Month', 'day' => 'Day',  'format' => 'yyyy-MM-dd',
  104.                               )))
  105.                      ->add('address')
  106.                      ->add('city')
  107.                      ->add('phone')
  108.                      ->add('email')
  109.                      ->add('job')
  110.                      ->add('doctor');
  111.     }
  112.  
  113.     public function configureOptions(OptionsResolver $resolver)
  114.     {
  115.         $resolver->setDefaults([
  116.             'data_class' => 'AppBundle\Entity\PersonMainInfo',
  117.         ]);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement