Symfony4 form handleRequest not submitting request data - forms

When i submit a json http post request in symfony4 the requesthandling not submitting my form.
I tried setting the "method" option to the form to post, but it doesn't seem to help. I'm using the application/json header for Content-type and Accept.
The form:
class SecurityUserForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SecurityUser::class,
'csrf_protection' => false,
]
);
}
}
The controller:
public function postAction(Request $request, SerializerInterface $serializer, UserPasswordEncoderInterface $encoder)
{
try {
$user = new SecurityUser();
$form = $this->createForm(SecurityUserForm::class, $user,
array(
'method' => 'POST'
));
$form->handleRequest($request); //NOT SUBMITTING THE FORM
if ($form->isSubmitted() && $form->isValid()) {
dump("submitted");
} else {
//Stuff
}
} catch (\Exception $e) {
//Exception handling stuff
}
}
And my http post:
Headers:
Content-Type:application/json
Accept:application/json
Body:
{
"username": "dunglas"
}
I expect the $form->handleRequest($request) to submit the form, but for some reason it doesn't. When i dump after handling the request
dump($form->isSubmitted()) //false
it returns false.
What am i doing wrong?

You need change $form->handleRequest($request); to $form->submit($request->request->all());

Related

Symfony 4 Form handling

I am not quite sure how symfony is providing the possibility of different forms for different users/views.
For my understanding you have the action:
public function new(Request $request): Response
{
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
return $this->render('orders/new.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
Which is generating the form with the, in this case OrdersType. And then the view gets loaded in the new.html.twig file. But some users only are allowed on the action "add" which should not show the option of calculated price or what ever.
How do I do that?
The solution depends of the structure of your application.
If the calculation price is in the new.html.twig you can use another file for users with limited access.
In the example below "ROLE_RESTRICTED" is the role of users allowed only to add and not to see the price.
For example :
public function new(Request $request): Response
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_index');
}
if($logged_user->hasRole('ROLE_RESTRICTED')){
$view = 'orders/newRestricted.html.twig'
}else{
$view = 'orders/new.html.twig';
}
return $this->render($view , [
'order' => $order,
'form' => $form->createView(),
]);
}
If the difference are small between new.html.twig and newRestricter.html.twig you can keep only one twig file and just make some conditional zones :
{% if not app.user.hasRole('ROLE_RESTRICTED') %}
Edit an order
{% else %}
Do not forget to secure the route corresponding to avoir direct URL access (in controller) :
public function edit($id)
{
$logged_user = $this->get('security.token_storage')->getToken()->getUser();
if($logged_user->has_role('ROLE_RESTRICTED')) {
throw $this->createAccessDeniedException('Access denied');
}
// create form, return render, etc.
}
In fact you have to restrict access to the page or part of the views.
You can find more details on the security section on the official doc :
https://symfony.com/doc/current/security.html
Another solution would be to make the filter in the form builder :
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class OrdersType extends AbstractType
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$logged_user = $this->tokenStorage->getUser();
if(!$logged_user->hasRole('ROLE_RESTRICTED')){
$builder->add('MyField', TextType::class,array(
'required' => false
));
}
}
...

How to structure Symfony to check for `onChange` dropdown fields?

In my example, I have four entities that I want to use for a blog platform: Owner Post User Location
An Owner can have multiple Posts
A Post can have multiple Users and Locations
I am trying to create an admin form where I can select the Owner from a dropdown menu, that will refresh the form onChange to populate the dropdown menu of Posts.
When a Post is then selected, the form is again refreshed with form fields Users and Locations associated to that post. I can then either:
Update the existing entities with more information (eg User's date of birth or Location's GPS coordinates)
Create a new instance of either entity to attach to the Post
I've not included the Location entity and not included the namespaces/include statements as they would detract from my main question of how such an admin page should be coded in the controller/Formtypes (my attempts are as follows):
Controller:
/**
* #Route("/adminposts", name="admin_posts")
*/
public function updatePostsAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($owner);
$em->persist($post);
$em->persist($user);
$em->persist($location);
$em->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('AppBundle:Default:adminupdate.post.html.twig', array(
'form' => $form->createView(),
));
}
User Formtype:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('owner','entity',array(
'class'=>'AppBundle:Owner',
'choice_label'=>'username',
'query_builder'=>function(EntityRepository $er) {
return $er->createQueryBuilder('d')
->orderBy('d.username','ASC');
}))
->add('post','entity',array(
'class'=>'AppBundle:Post',
'choice_label'=>'posttext',
'query_builder'=>function(EntityRepository $er) {
return $er->createQueryBuilder('d')
->orderBy('d.postdate','ASC');
}))
->add('Firstname')
->add('Surname')
->add('DOB')
->getForm();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Users',
));
}
public function getName()
{
return 'user';
}
}
Ok, maybe you can take a look here : Symfony2: Change choices with ajax and validation or follow my method :
In your UserType :
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('owner','entity',array(
'class'=>'AppBundle:Owner',
'choice_label'=>'username',
'query_builder'=>function(EntityRepository $er) {
return $er->createQueryBuilder('o')
->orderBy('o.username','ASC');
}))
->add('Firstname')
->add('Surname')
->add('DOB');
// Add listeners for Post field
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
}
protected function addElements(FormInterface $form, $owner = null)
{
if($owner){
$form->add('post','entity',array(
'class'=>'AppBundle:Post',
'choice_label'=>'posttext',
'query_builder'=>function(EntityRepository $er, $owner) {
return $er->createQueryBuilder('p')
->join('p.owner', 'o')
->where('o.id = :ownerID')
->setParameter('ownerID', $owner->getID() )
->orderBy('d.postdate','ASC');
}));
}
else{
$form->add('post','choice',array(
'choice_label'=>'posttext',
'empty_value' => '-- Choose --',
'choices' => array())
);
}
}
public function onPreSubmit(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
$this->addElements($form, $data->getOwner());
}
public function onPreSetData(FormEvent $event)
{
/** #var User user */
$user = $event->getData();
$form = $event->getForm();
$this->addElements($form, $user->getOwner());
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Users',
));
}
public function getName()
{
return 'user';
}
}
I assume you know how to create an onChange Event in jQuery (I use to use it) and an ajax call.
Just for remind, in your view :
{{ form_row(
form.owner,
{
'attr': {
'data-owner-id': ""~form.owner.vars.id,
'class': "change-posts-per-owner",
}
}
) }}
{{ form_row(form.post) }}
{{ form_row(form.firstname) }}
{{ form_row(form.surname) }}
{{ form_row(form.DOB) }}
And your Ajax Script :
$(document).on('change', 'select .change-posts-per-owner', function(){
var ownerID = $(this).data("owner-id");
$.ajax({
url: your_url,
type: "GET", //or POST
data: 'ownerID='+ownerID,
dataType: 'JSON', //or html or whatever you want
success:function(data) {
//Replace <option element in your select element for post list considering your dataType (type of response)
}
});
}
You also can use $.post() or $.get() instead of explicit method $.ajax();
I sugget you to use FOSJSRoutingBundle for indicating your url with Routing.generate() method.

How to add data transformer inside a event listener

So, here's my problem: I have to add a field to a form based on it's underlying data but i've to add a data transformer to the field.
I thought the solution will be simple, just add a PRE_SET_DATA event listener to the form (just to access to the underlying data) and add the field and the transformer inside the listener. But i can't add the transformer inside the listener because the form is already locked.
I´ve tried many workarounds but i couldn't solved it. Her's my code:
$builder->...
->add(
$builder->create('date', 'choice', array(
'label' => 'form.game.date',
'empty_value' => 'form.game.date',
'required' => false,
'choices' => array(
'2014-04-10' => '10/Apr', // just to test
'2014-04-11' => '11/Apr',
)
))
->addModelTransformer(new DateToStringTransformer())
);
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder) {
$game = $event->getData();
$form = $event->getForm();
$period = new \DatePeriod(
$game->getTournament()->getBeginDate(),
new \DateInterval('P1D'),
$game->getTournament()->getEndDate()->add(new \DateInterval('P1D'))
);
$dates = array();
foreach($period as $date){
$dates[$date->format("Y-m-d")] = $date->format("j/M");
}
$form->add('date', 'choice', array(
'label' => 'form.game.date',
'choices' => $dates,
));
});
When i add the date field to the form inside event listener, the previously added data field is replaced so it's data transformer...
Is there a way to do it?
I wrote some test and updated your code a bit. Check if i understand your question correctly.
SomeTypeTest.php:
<?php
class SomeTypeTest extends TypeTestCase
{
/**
* #test
*/
public function testSubmitValidData()
{
$begin = new \DateTime();
$formData = array(
'date' => '2014-01-15'
);
$type = new SomeType();
$form = $this->factory->create($type);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals(['date' => \DateTime::createFromFormat('Y-m-d', '2014-01-15')], $form->getData());
$view = $form->createView();
$children = $view->children;
foreach (array_keys($formData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}
}
SomeType.php:
<?php
class SomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder) {
//$game = $event->getData();
$form = $event->getForm();
$period = new \DatePeriod(
\DateTime::createFromFormat('Y-m-d', '2014-01-01'), // for test
new \DateInterval('P1D'),
\DateTime::createFromFormat('Y-m-d', '2014-01-30') // for test
);
$dates = array();
foreach ($period as $date) {
$dates[$date->format("Y-m-d")] = $date->format("j/M");
}
$form->add($builder->create('date', 'choice', array(
'label' => 'form.game.date',
'empty_value' => 'form.game.date',
'auto_initialize' => false,
'required' => false,
'choices' => $dates
))
->addModelTransformer(new DateToStringTransformer())->getForm()
);
});
}
public function getName()
{
return 'st';
}
}
DateToStringTransformer.php:
<?php
class DateToStringTransformer implements DataTransformerInterface
{
/**
* #param mixed $value
* #return mixed|void
*/
public function transform($value)
{
if (!$value) {
return null;
}
return $value->format('Y-m-d');
}
/**
* #param mixed $value
* #return mixed|void
*/
public function reverseTransform($value)
{
return \DateTime::createFromFormat('Y-m-d', $value);
}
}
https://gist.github.com/ChubV/11348928
I've managed to get it work by creating a custom type that always add the data transformer. Then i can call "form->add('date', 'my_type',..)" from any event listener without loosing the data transformer.
MyType.php
class MyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field1')
->add('field2')
...;
$builder->addEventSubscriber(new AddDateSubscriber());
}
}
CustomType.php
class DateChoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new DateToStringTransformer());
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'invalid_message' => 'The selected date does not exist',
));
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'date_choice';
}
}
Every time i add a date_choice type to a form the data transformer will be added too.
class AddDateSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
public function preSetData(FormEvent $event)
{
$game = $event->getData();
$form = $event->getForm();
$endDate = \DateTime::createFromFormat('Y-m-d', $game->getTournament()->getEndDate()->format('Y-m-d'));
$period = new \DatePeriod(
$game->getTournament()->getBeginDate(),
new \DateInterval('P1D'),
$endDate
);
$dates = array();
foreach($period as $date){
$dates[$date->format("Y-m-d")] = $date->format("j/M");
}
$form->add('date', 'date_choice', array(
'label' => 'form.game.date.label',
'empty_value' => 'form.game.date.none',
'required' => false,
'choices' => $dates,
));
}
}
DateToStringTransformer.php
class DateToStringTransformer implements DataTransformerInterface
{
public function transform($date)
{
if (null === $date) {
return "";
}
return $date->format("Y-m-d");
}
public function reverseTransform($stringDate)
{
if (!$stringDate) {
return null;
}
$date = \DateTime::createFromFormat('Y-m-d', $stringDate);
if (false === $date) {
throw new TransformationFailedException('Sting to date transformation failed!');
}
return $date;
}
}
Hope that this will help someone.

Symfony2 - Set a selected value for the entity field

I'm trying to set a selected value inside an entity field. In accordance with many discussions I've seen about this topic, I tried to set the data option but this doesn't select any of the values by default:
class EventType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('place', 'entity', array(
'class' => 'RoyalMovePhotoBundle:Place',
'property' => 'name',
'empty_value' => "Choisissez un club",
'mapped' => false,
'property_path' => false,
'data' => 2
))
->add('begin')
->add('end')
->add('title')
->add('description')
;
}
// ...
}
By looking for more I've found that some people had to deactivate the form mapping to the entity. That seems logical so I tried to add 'mapped' => false to the options, without success...
If it can help, here's my controller:
class EventController extends Controller
{
// ...
public function addAction()
{
$request = $this->getRequest();
$em = $this->getDoctrine()->getManager();
$event = new Event();
$form = $this->createForm(new EventType(), $event);
$formHandler = new EventHandler($form, $request, $em);
if($formHandler->process()) {
$this->get('session')->getFlashBag()->add('success', "L'évènement a bien été ajouté.");
return $this->redirect($this->generateUrl('photo_event_list'));
}
return $this->render('RoyalMovePhotoBundle:Event:add.html.twig', array(
'form' => $form->createView()
));
}
}
And the EventHandler class:
class EventHandler extends AbstractHandler
{
public function process()
{
$form = $this->form;
$request = $this->request;
if($request->isMethod('POST')) {
$form->bind($request);
if($form->isValid()) {
$this->onSuccess($form->getData());
return true;
}
}
return false;
}
public function onSuccess($entity)
{
$em = $this->em;
$em->persist($entity);
$em->flush();
}
}
I'm a bit stuck right now, is there anyone who got an idea?
You only need set the data of your field:
class EventController extends Controller
{
// ...
public function addAction()
{
$request = $this->getRequest();
$em = $this->getDoctrine()->getManager();
$event = new Event();
$form = $this->createForm(new EventType(), $event);
// -------------------------------------------
// Suppose you have a place entity..
$form->get('place')->setData($place);
// That's all..
// -------------------------------------------
$formHandler = new EventHandler($form, $request, $em);
if($formHandler->process()) {
$this->get('session')->getFlashBag()->add('success', "L'évènement a bien été ajouté.");
return $this->redirect($this->generateUrl('photo_event_list'));
}
return $this->render('RoyalMovePhotoBundle:Event:add.html.twig', array(
'form' => $form->createView()
));
}
}
In order to option appear selected in the form, you should set corresponding value to entity itself.
$place = $repository->find(2);
$entity->setPlace($place);
$form = $this->createForm(new SomeFormType(), $entity);
....
For non-mapped entity choice fields, the method I found easiest was using the choice_attr option with a callable. This will iterate over the collection of choices and allow you to add custom attributes based on your conditions and works with expanded, multiple, and custom attribute options.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('place', 'entity', array(
//...
'choice_attr' => function($place) {
$attr = [];
if ($place->getId() === 2) {
$attr['selected'] = 'selected';
//for expanded use $attr['checked'] = 'checked';
}
return $attr;
}
))
//...
;
}
When you use the query_builder option, and the data option expects an collection instance, and you don't want to touch your controller by adding setDatas for only certain fields, and you already have your querybuilder and the ids of the repopulating options in your form type class, you can repopulate a selection as following:
// Querybuilder instance with filtered selectable options
$entities = $qb_all;
// Querybuilder instance filtered by repopulating options (those that must be marked as selected)
$entities_selected = $qb_filtered;
Then in your add() Method
'data' => $entities_selected->getQuery()->getResult(), // Repopulation
'query_builder' => $entities,
EDIT: Real use case example
You want to repopulate a checkbox group rendered with following elements:
Label: What is your favourite meal?
4 Checkboxes: Pasta, Pizza, Spaghetti, Steak
And you want to repopulate 2 Checkboxes:
Pizza, Steak
$qb_all would be a QueryBuilder instance with the all 4 selectable Checkboxes
$qb_filtered would be a new additional QueryBuilder instance with the repopulating Checkboxes Pizza, Steak. So a "filtered" version of the previous one.

Model populated with unposted data

I have two models, Lesson and Evaluation. Each lesson can have multiple evaluations.
I am trying to set up an embedded form which will allow users to enter all of this data at the same time.
It works fine for adding and editing data, however I have a problem if I try to remove an evaluation.
For example, I have a lesson with three evaluations attached. I then submit with form again but with one of those removed.
In the controller, I first get the lesson being edited, then get its evaluations and loop through them, printing the ids. Three ids are printed as expected.
Next, I bind the request to the form and check if it is valid. I then get the evaluations again and loop through them once more to check that they've been removed, however all three ids were still there!
If I print the raw POST data, there are only two.
Can anyone see what I have done wrong?
Here is my controller code:
public function editAction($id = NULL)
{
$lesson = new Lesson;
if ( ! empty($id))
{
$lesson = $this->getDoctrine()
->getRepository('LessonBundle:Lesson')
->find($id);
}
foreach ($lesson->getEvaluations() as $evaluation)
{
print_r($evaluation->getId());
print_r('<br />');
}
$form = $this->createForm(new LessonType(), $lesson);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
foreach ($lesson->getEvaluations() as $evaluation)
{
print_r($evaluation->getId());
print_r('<br />');
}
die();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($lesson);
$em->flush();
}
}
}
Here is my lesson form:
class LessonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('evaluations', 'collection', array(
'type' => new EvaluationType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
));
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'LessonBundle\Entity\Lesson',
);
}
public function getName()
{
return 'Lesson';
}
}
And finally, my Evaluation form:
class EvaluationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('report');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'LessonBundle\Entity\Evaluation',
);
}
public function getName()
{
return 'Evaluation';
}
}
Any advice appreciated.
Thanks.
I think your form is not binded properly with the Class.
have a look at here http://symfony.com/doc/2.0/cookbook/form/form_collections.html
Form should look like this
$builder
->add('name lesson')
->add('evaluation', 'collection', array('type'=>new EvaluationsType()))
YOu need to create a new form class with other forms embedded.
The other way is to manually check the posted data and remove the evalauation manually in the controller and then persist the Lesson again
I got around in by using:
$lesson = $form->getData();