How to submit form in embeded controller? - php

I want to make a newsletter in a footer with my service. To do that, I have to embed a controller in a view :
{{ render(controller('AppBundle:RegisterNewsletter:registerToTheNewsletter')) }}
The probleme is that when I submit my form, the page is refreshed but nothing else, even I insert a "die;"
public function registerToTheNewsletterAction(Request $request)
{
$form = $this->createFormBuilder()
->add('email', EmailType::class)
->add('subscribe', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
var_dump($data);
die;
}
return $this->render('include/registerNewsletter.html.twig', array('form' => $form->createView()));
}
Thanks for your help

I've got a solution, when I said, I had to embed a controller in a view. In fact during the submitting, the page is refreshed and the data is lost.
So I change the action of the form to redirect the data on an other path, the data is processed in an other action and I redirect on the referer.
If you want more explains, ask me

Related

Symfony 3.4 - how are forms whose actions point to a different controller handled?

I want to process a form in a separate controller. While the Symfony docs show how to change a form's action and method, they don't show how the controller selected in $form->setAction() is actually supposed to handle the form. Is it present in Request? Do we make another Form object so we can check $form->isSubmitted() and $form->isValid()?
It's a pretty glaring omission.
Here is a quick example:
Controller for displaying the form
/**
* #route("/form", name="form_route")
*/
public function formAction()
{
$form = $this->createFormBuilder()
->setAction($this->generateUrl('task_route'))
->setMethod('POST')
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class)
->getForm();
return $this->render('form.html.twig', [
'form' => $form->createView(),
]);
}
Controller to handle submission
Is it present in Request?
Yes, the form data is in the request.
Do we make another Form object so we can check $form->isSubmitted() and $form->isValid()?
The form has to be recreated so that you can handle and validate the request.
/**
* #route("/task", name="task_route")
*/
public function postAction(Request $request)
{
$form = $this->createFormBuilder()
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$task = $form->getData();
/* ... */
}
//render task to view submission
return $this->render('task.html.twig', [
'task' => $task,
]);
}
This does have some duplicate code even when using entities and Symfony's Form Classes, which is why Symfony Docs recommends using the same controller for processing forms.

Modifying Symfony form data after failed validation

I've got a Symfony form field with a custom validator. If the user submits the form and validation fails, I'd like to correct the value and show it to the user for review.
How can I modify a submitted form field after validation?
PRE_SUBMIT isn't suitable as it's executed before the validation:
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
$data = $event->getData();
$data['myField'] = 'Modified!';
$event->setData($data);
});
I've also tried making the modification in the controller, but I get a You cannot change the data of a submitted form error.
if ($form->isSubmitted() && !$form->isValid()) {
$form->get('myField')->setData('Modified!');
}
Is there any way of doing this?
How is this way?
$myValue = '';
if ($form->isSubmitted() && !$form->isValid()) {
$myValue = 'Modified!';
}
return $this->render('my_template.html.twig', [
'form' => $form->createView(),
'myValue' => $myValue,
]);
In my_template.html.twig,
{{ form_widget(form.myField, {'value' : myValue}) }}

Symfony API Request Unexpected "G"

I'm really thrown off by this error I get when posting to Google Books API. The last response in this function is triggered when I post 1781100489 to a form. This form then runs resultAction function. Instead of returning what I want it to, I get a JSON.parse error with an unexpected token in line 1 column 1.
When tested with Postman, it gives me this peculiar message in its response Unexpected 'G'. What could this mean? I have been dumping the variable as you can see, it's commented, but can't understand why this is occuring.
Any clues?
public function findAction(Request $request)
{
$form = $this->createFormBuilder(null, ['csrf_protection' => false])
->add('Title', TextType::class)
->add('Search', SubmitType::class)
->getForm();
$form->handleRequest($request);
if($form->isValid()){
$json = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=".$form->get('Title')->getData());
$response = json_decode($json, true);
if(array_key_exists('items', $response)){
return $this->render('BlogsiteBooksBundle:Pages:results.html.twig', [
'items' => $response['items']
]);
} else {
return new Response('Google did not have any items', 400);
}
}
//var_dump($request);
return new Response('Google Book Not Found', 404);
}
Your request is not related to the form, build the form using Symfony form component in the controller, and manually in the twig, then parameters names does not match. The $form->handleRequest($request); can't find the title, because expect a parameter called form[Title] and you are passing only Title.
Solutions:
1 - Submit manually all given parameters in the query
Change:
$form->handleRequest($request);
For this:
$form->submit($request->query->all());
2 - Change the name of the input to match with symfony expected name and the method used should be POST:
<form action="app_dev.php/find" method="post">
<input type="text" name="form[Title]" id="Title" value="">
<input type="submit" value="Search">
</form>
3 - Render your form using twig form to avoid this problems
Controller:
/**
* #Route(name="find", path="/find")
*/
public function findAction(Request $request)
{
$form = $this->createFormBuilder(null)
->add('Title', TextType::class)
->add('Search', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$json = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=".$form->get('Title')->getData());
$response = json_decode($json, true);
if (array_key_exists('items', $response)) {
return $this->render(
'BlogsiteBooksBundle:Pages:results.html.twig',
[
'items' => $response['items'],
]
);
} else {
$form->addError(new FormError('Google did not have any items'));
}
}
return $this->render('find.html.twig', ['form' => $form->createView()]);
}
View find.html.twig:
{{ form(form) }}
Note: each point is a possible solution, use only one of them, the third is the recommended for simple Symfony forms.
I was dealing with the same error in POSTMAN. I searched for answers but nothing was satisfying my problem.
I find by self that their is a statement "exit();" in my PHP file. I removed it & my problem was solved. I hope my solution help someone.

How to use nested form handlers in Symfony 3

I'm learning Symfony to move an old website from flat-PHP to a framework.
In the old website I have a page with two levels of forms: the first one is just a button with which the user accepts some conditions, while the second one sends a message to the website admin. When the user clicks the button in the first form, the page is refreshed and the second form appears. The important thing is that the user can't get access to the second form without pushing the first button.
Right now, the action method is this one:
/**
* #Route("/ask-consultation", name="ask_consultation")
*/
public function askConsultationAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$form = $form = $this->createFormBuilder()
->add('submit', SubmitType::class, array('label' => 'Confermo'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$consultation = new Consultation;
$form = $this->createForm(AskConsultationType::class, $consultation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$consultation = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($consultation);
$em->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('visitor/consultation/ask_step2.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
'sidebars' => $em->getRepository('AppBundle:Sidebar')->findAllOrderedBySortingPosition(),
'form' => $form->createView(),
]);
}
When I go to /ask-consultation it shows me the button to accept certain conditions; when I click the button, it shows me the form to send the message, but when I send it, I don't get redirected to homepage, but again to the first page of /ask-consultation.
I understand why this code doesn't work, but I can't understand how to make it work. One solution could be some sort of modal dialog for the first form, but if possible I'd prefer to handle all the passages in PHP. Is it possible to split the form handling without changing the route?
The most important thing in my case is that the user can't get to the second form without first having clicked on the first button.
Render everything in a single form but use javascript (jQuery) to hide the ask-consultation part of the form. When the user clicks "Confirm", then unhide the form. This avoids the controller altogether for the confirmation step. You don't seem to be recording this confirmation anyway, so just let the client side handle things. You can still check (on form submission) to see that the "confirmation" was accepted, for example by setting a hidden variable through javascript.
It turns out the answer was quite simple.
In my mind, I had to handle the second form inside the first if ($form->isSubmitted() && $form->isValid()), but of course that's not the path taken by PHP. Everytime the route gets loaded, askConsultationAction() is run from its first instruction. I simply had to initialize both forms (with different variable names, obviously), and handle the forms one after the other, rather than one inside the other.
This code works like a charm:
/**
* #Route("/ask-consultation", name="ask_consultation")
*/
public function askConsultationAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$consultation = new Consultation;
$conditions_form = $this->createFormBuilder()
->add('submit', SubmitType::class, array('label' => 'Confermo'))
->getForm();
$consultation_form = $this->createForm(AskConsultationType::class, $consultation);
$consultation_form->handleRequest($request);
$conditions_form->handleRequest($request);
if ($conditions_form->isSubmitted() && $conditions_form->isValid()) {
return $this->render('visitor/consultation/ask_step2.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
'sidebars' => $em->getRepository('AppBundle:Sidebar')->findAllOrderedBySortingPosition(),
'form' => $consultation_form->createView(),
]);
}
if ($consultation_form->isSubmitted() && $consultation_form->isValid()) {
$consultation = $consultation_form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($consultation);
$em->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('visitor/consultation/ask_step1.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
'sidebars' => $em->getRepository('AppBundle:Sidebar')->findAllOrderedBySortingPosition(),
'form' => $conditions_form->createView(),
]);
}
Maybe, it would be even better if I used an else... if....

Form Symfony 2.3 subscription

I am doing a form with Symfony 2.3 to suscribe to a newsletter.
The form is working good in is own template (newsletter.html.twig).
My controller action:
public function newsletterAction(Request $request)
{
$newsletter = new Newsletter();
$form = $this->createFormBuilder($newsletter)
->add('email', 'email')
->add('submit', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($newsletter);
$em->flush();
$request->getSession()->getFlashBag()->add('notice', 'Vous venez de vous enregistré à la Newsletter d\'Emoovio.');
}
return $this->render('MyBundle:Global:newsletter.html.twig', array(
'form' => $form->createView(),
));
}
My template where it's working (newsletter.html.twig) :
{{ form(form) }}
My template where it does not work (index.html.twig):
////
{% render (controller("EmooviofrontBundle:Global:newsletter")) %}
////
The form is display but it's not working. May be is miss something. Has anyone had the same problem and could explain me. Thank you.
I think that when you submit your form, the POST data arrives in the indexAction. This action does not validate your form and renders the normal page. While rendering it will call the Global:newsletterAction but just as a sub request in the GET method without form data.
You could try to apply an action to your formdata
$form = $this->createFormBuilder($newsletter, array(
'action' => $this->generateUrl('global_newsletter'),
'method' => 'POST',
))
->add('email', 'email')
->add('submit', 'submit')
->getForm();

Categories