I created a form in the FOSUserbundle:Profile:showcontent.html.twig
file. I want to know if a users want's to subscribe to the newsletter or not.
<form action="{{ path('fos_user_newsletter') }}" method="POST"
class="fos_user_newsletter" id="fos_user_newsletter">
<input type="checkbox" name="test123" value="HTML"
{% if user.newsletterEnabled == 1 %} checked
{% endif %} onclick="this.form.submit();"> Subscribe to Newsletter
</form>
Now I would like to get the data in the controller.
public function subscribeNewsletterAction(Request $
var_dump($request->request->all());die;
}
This doesn't display anything and I can't call the $form = $this->createFormBuilder($task)
because it's in FOSUserBundle
.
How can I get the information if the checkbox is clicked or not? What thinking mistake am I doing?
To be clear, if you created this form in the FOSUserBundle/Resources/views/Profile/show_content.html.twig
file, than it's wrong. You should not edit anything inside the vendor
folder, as they can be overwritted by Composer.
You have to put this form into a template in the src
directory.
You should create a simple form with Symfony to do this job.
// make sure you've imported the Request namespace above the class
use Symfony\Component\HttpFoundation\Request;
// ...
public function subscribeNewsletterAction(Request $request)
{
$form = $this->createFormBuilder($defaultData)
->add('newsletter', 'choice', array(
// ...
))
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
}
// ... render the form
}