M
M
magary42017-01-12 11:01:49
symfony
magary4, 2017-01-12 11:01:49

When not to use forms?

If you need to send one text field, well, or 2 - one text field, the second is a checkbox, let's say this is a search page
Does it make sense to create all this

$form = $formFactory->createBuilder()
    ->add('task', TextType::class)
    ->getForm();

$form->handleRequest($request);

if ($form->isSubmitted()) {
    $data = $form->getData();

    // ... perform some action, such as saving the data to the database

    $response = new RedirectResponse('/task/success');
    $response->prepare($request);

    return $response->send();
}

and then another {{ form_widget(form) }}
??
I don’t see it
for myself, I decided that if the form is not saved anywhere and you don’t need to open it for editing, then read it directly from $request->get("input_name")
and only html in the twig,
correct me if I'm wrong

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Svirsky, 2017-01-12
@e_svirsky

Well don't use Symfony Forms for your case if you don't want to. You can also get GET, POST parameters from superglobal arrays and not use the $request object, which already returns satinized data.
IMHO the question is in your regard. Today the form has 2 fields, and tomorrow you need 3 more, then 2 more, and that's it. You will have to refactor. I do not understand what the problem is to lay it right away?
As for me, the forms in the symphony are quite worthy.
And don't create the form on the fly in the controller - that's bad form. It is better to keep the forms in a separate class and give the processing to the manager when submitting. Thus, the controller will not twitch when increasing the functionality, and should not.

V
voronkovich, 2017-01-13
@voronkovich

Yes, in your case you don't need to use the forms component at all. One of the tasks of a form is to map data into an object. As far as I understand, you do not need a mapping, which means that the form is not needed either. One example of when forms should not be used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question