简体   繁体   中英

How do I catch form data in my controller using a class object and not the traditional getData() Method in symfony

I have created a search form in which I use to search my database to produce some result.

using the below in a my forms directory as a form class I generate the form.

   $builder->add(
            'startDate',
            DateType::class,
            [
                'label' => 'start date',
                'format' => 'yyyy-MM-dd',
                'required' => true,
                'constraints' => [
                    new Constraints\NotBlank(),
                    new Constraints\DateTime(),
                ],
            ]
        );

in my controller I already have retrieved the data using the getData() method

 $form = $this->createForm(testForm::class);
        $form->handleRequest($request);

 if ($form->isSubmitted() && $form->isValid()) {
          
            $start = date_format($form->get('start')->getData(), 'Y-m-d');

At the moment I want to get this data using a class object

eg like this

   $form = $this->createForm(testForm::class, $classobject);
            $form->handleRequest($request);

where I would use the class object to retrieve the posted data from the form class "testForm"

how have I tried to solve this on my own?

I tried reading tutorials on this concept eg as below

https://blog.martinhujer.cz/symfony-forms-with-request-objects/

note: this is a learning curve for me, I do not really grasp this concept

Please, constructive responses would be well appreciated.

Thanks

$form->getData() will return the object populated with the submited data. By default the Form Component returns an array, but if you pass an object as the second arg to createForm or set data_class option then you will get the object back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM