Skip to content

Form data

Samvdg edited this page Mar 2, 2020 · 20 revisions

How to manipulate form data First things first First thing to check if we're manipulating form data is to decide where in the sequence to manipulate said data. For now we can do it after the submission. i'll take the create function from the symfony crud as an examnple.

Lets take a look at the following example of students and teachers:


   /**
     * @Route("/new", name="student_new", methods={"GET","POST"})
     * Bij deze functie voor het nieuw aanmaken van een student wordt eerst nagelopen of het teacher is gesloten doormiddel van de 
     * gesubmitte data langs te lopen, daarnaast checkt hij ook of na het aanmaken van de student deze het max aantal studenten heeft bereikt
     */
    public function new(StudentRepository $studentRepository, TeacherRepository $TeacherRepository, Request $request): Response
    {
        $student= new Student();
        $form = $this->createForm(StudentType::class, $student);
        $form->handleRequest($request);
        $em = $this->getDoctrine()->getManager();
        
        // als het form goed is
        if ($form->isSubmitted() && $form->isValid()) {
            // haal het id op van het gegeven teacher
            $teacher = $form->getData()->getTeacher();
            $count = 0;
            $counts = $em->getRepository('App:Student')->findBy(['student' => $teacher->getId()]);
            // dd(count($count));
            foreach($counts as $student)
            {
                $count++;
            }
            // dd($count);
            if($gesloten == 1 || $count >= $teacher->getMaxPlayers())
            {
                return $this->render('student/index.html.twig',[
                    'students' => $studentRepository->findAll(),
                    'status' => '404',
                ]);
            }
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($student);
            $entityManager->flush();

            $counts = $em->getRepository('App:Student')->findBy(['teacher' => $teacher->getId()]);
            $count = 0;
            foreach($counts as $student)
            {
                $count++;
            }
            if($count = $teacher->getMaxPlayers()){
                return $this->render('student/index.html.twig',[
                    'students' => $studentRepository->findAll(),
                    'status' => '405',
                ]);
            }


            return $this->redirectToRoute('aanmelding_index');
        }

        return $this->render('student/new.html.twig', [
            'student' => $student,
            'form' => $form->createView(),
        ]);
    }

its pretty basic but looks complicated. The first thing we check is the data given to us by the form. We collect that as soon as it's submitted, thats why it's in the 'if' statement:

if ($form->isSubmitted() && $form->isValid()) { $gesloten = $form->getData()->getTeacher()->getGesloten(); 

after that... we have the data! You can do with it whatever you would like. In the example it will have conditional rendering as you can see with another 'if' statement. if($gesloten == 1) But that doesn't always have to be the case ofcourse! t=t a=s

Clone this wiki locally