0

I am using a hybrid of Symfony forms only for submit and cancel buttons, where any user input data is sent via HTML.

Here is my Controller Code that gets the request. It retrieves whether or not the save button was clicked fine but it passes in null for $content

$form = $this->createForm(FormType::class)
        ->add('save', SubmitType::class, array('label'=> 'Save changes form',
            'attr' => ['class' => 'btn btn-primary']));
    $form->handleRequest($request);
    if ($form->get('save')->isClicked()){
        if ($form->isValid()){
            $content = $request->get('content');
            $announcementToCreate = new Announcement($content, $currentUser);
            $currentUser->getAnnouncements()->add($announcementToCreate);
            $this->getDoctrine()->getManager()->persist($announcementToCreate);
            $this->getDoctrine()->getManager()->flush();

Here is my twig/html code. I am using a modal for this form. I was able to get the desired results using pure html.

<div class="modal-body">
    {{ form(form) }}
    {{ form_start(form) }}
        <textarea class="form-control" id="announcementText" rows="5" name="contents"></textarea>
        {{ form_end(form) }}

1 Answer 1

2
  1. You need to add your textarea input to the Form:
$form = $this->createForm(FormType::class)
    ->add('content', TextareaType::class, [
        'label' => false,
        'attr' => [
            'rows' => 5,
            'class' => 'form-control'
        ]
    ])
    ->add('save', SubmitType::class, [
        'label'=> 'Save changes form',
        'attr' => ['class' => 'btn btn-primary']
    ]);

  1. {{ form(form) }} is used for a complete form render, you don't need to use it alongside with form_start/form_end.

Just do a complete form render:

{{ form(form) }}

or render each part of the form individually:

{{ form_start(form) }}

  {{ form_row(form.content) }}
  {{ form_row(form.submit) }}

{{ form_end(form) }}
  1. You don't need to check your button for a "click", just check if your form was submitted:
...

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

    // You can get your data directly from the form:   
    $content = $form->get('content')->getData();

    $announcementToCreate = new Announcement($content, $currentUser);
    $currentUser->getAnnouncements()->add($announcementToCreate);
    $this->getDoctrine()->getManager()->persist($announcementToCreate);
    $this->getDoctrine()->getManager()->flush();
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the response. is there any way to achieve the results without using Symfony forms for the text input and just html? We are trying to avoid using Symfony forms except for security purposes
@d1596 Yeah, just put your textarea between form_start and form_end and get your data through the $request in the controller. Or you can just use the plain <form> and <submit> tags in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.