embedding current user id in a form Symfony 2 - php

I am trying to validate a form in Symfony 2.3. I need to get the current User id and submit in my database.
My controller :
public function giftcardAction(Request $request)
{
$giftcard = new Giftcard();
$giftcard->setDate(new \DateTime());
$user = $this->container->get('security.context')->getToken()->getUser();
$userId = $user->getId();
var_dump($userId);
$form = $this->createFormBuilder($giftcard)
->add('amount', 'integer')
->add('onBehalfOf')
->add('towards')
->add('message', 'text')
// ->add($userId)
->add('submit', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($giftcard);
$em->flush();
}else{
throw new NotFoundHttpException("Page not found");
}
}
return $this->render('FrontBundle:Forms:giftcard.html.twig', array(
'form' => $form->createView(),
));
}
My FormType
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class GiftCardType extends AbstractType
{
private $userId;
public function __construct(array $userId)
{
$this->userId = $userId;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date')
->add('onBehalfOf')
->add('towards')
->add('message')
// ->add('UserId')
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Front\Bundle\Entity\GiftCard',
));
}
/**
* #return string
*/
public function getName()
{
return 'front_bundle_giftcard';
}
}
At the moment i can get the user id in var_dump in the controller. But how I can pass the variable in the form. Has anyone may help me. Thank you.

Related

Why did Symfony forms does not using validator normally?

I have the collection model:
class EntityList {
/**
* #var Entity[]
* #Assert\Count(min=1)
* #Assert\Valid()
*/
private $entityList;
// ...
}
The single model:
class Entity {
/**
* #var int
* #Assert\NotBlank()
*/
private $id;
/**
* #var string
* #Assert\NotBlank()
*/
private $name;
// ...
}
The collection's form:
class EntityListType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('entity', CollectionType::class, [
'entry_type' => EntityType::class,
'allow_add' => true,
'property_path' => 'entityList',
]);
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefault('data_class', EntityList::class);
}
public function getBlockPrefix() {
return null;
}
}
And the entity's form:
class EntityType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('id', NumberType::class)
->add('name', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefault('data_class', Entity::class);
}
}
The collection's form using in a controller:
$form = $this->createForm(EntityListType::class);
$form->handleRequest($request);
I send the package with no required title (it's an API):
curl -X POST ...
-d entity[0][id]=10
And that accept it! The method isValid returns true. But if form's data check manually with ValidationComponent as:
if ($form->isValid()) { // it's valid, okay
echo $this->get('validator')->validate($form->getData()); // it's still not valid!
}
I see correct errors ('title is required')! Why?
P.S. Symfony 3.2, stable.
P.P.S. I was found temporarily solution now. It's use $form->submit($request->request->all()); instead of $form->handleRequest($request);. But I don't understand this feature.

Symfony3 wont convert file to UploadedFile

I'm following this tutorial http://symfony.com/doc/current/controller/upload_file.html but the $file isn't being converted to an UploadedFile, it stays as a string:
var_dump($file);
/home/owner/Desktop/Workspace/Documentary/src/DW/DocumentaryBundle/Controller/Admin/DocumentaryController.php:103:string '/tmp/phpmmv1LP' (length=14)
Type error: Argument 1 passed to DW\DocumentaryBundle\Uploader\PosterUploader::upload() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, called in /home/owner/Desktop/Workspace/DocumentaryWIRE/src/DW/DocumentaryBundle/Controller/Admin/DocumentaryController.php on line 106
Create Action:
/**
* #param Request $request
* #return Response
*/
public function createAction(Request $request)
{
$documentary = new Documentary();
$form = $this->createForm(DocumentaryType::class, $documentary);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $documentary->getPoster();
$posterUploader = $this->getPosterUploader();
$fileName = $posterUploader->upload($file);
$documentary->setPoster($fileName);
$documentaryService = $this->getDocumentaryService();
$documentaryService->save($documentary);
}
return $this->render('DocumentaryBundle:Admin:create.html.twig', array(
'form' => $form->createView(),
));
}
View
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
Form:
<?php
namespace DW\DocumentaryBundle\Form;
use DW\DocumentaryBundle\Entity\Documentary;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DocumentaryType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class)
->add('storyline', TextareaType::class)
->add('summary', TextareaType::class)
->add('year', IntegerType::class)
->add('length', IntegerType::class)
->add('status', TextType::class)
->add('views', IntegerType::class)
->add('shortUrl', TextType::class)
->add('videoId', TextType::class)
->add('videoSource', TextType::class)
->add('poster', FileType::class, [
'label' => 'Poster'])
->add('category', EntityType::class, [
'class' => 'CategoryBundle:Category',
'choice_label' => 'name',
])
->add('submit', SubmitType::class);
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Documentary::class,
));
}
/**
* #return string
*/
public function getName()
{
return "documentary";
}
}
Controller:
<?php
namespace DW\DocumentaryBundle\Uploader;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class PosterUploader
{
/**
* #var string
*/
private $targetDir;
/**
* #param string $targetDir
*/
public function __construct(string $targetDir)
{
$this->targetDir = $targetDir;
}
/**
* #param UploadedFile $file
* #return string
*/
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->targetDir, $fileName);
return $fileName;
}
}
The documentation is NOT wrong! They don't use type hinting in their example:
// src/AppBundle/Entity/Product.php
public function setBrochure($brochure)
{
$this->brochure = $brochure;
return $this;
}
When you use string type hinting then UploadedFile::__toString() will be invoked!
So check your entity. It should be something like this:
public function setPoster($poster)
{
// ...
}
To get a file from request, in Symfony, you need to get $request->files. For example $request->files->get('file_name');.
The documentation is wrong, it should be: $file = $form->get('poster')->getData();
/**
* #param Request $request
* #return Response
*/
public function createAction(Request $request)
{
$documentary = new Documentary();
$form = $this->createForm(DocumentaryType::class, $documentary);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $form->get('poster')->getData();
$posterUploader = $this->getPosterUploader();
$fileName = $posterUploader->upload($file);
$documentary->setPoster($fileName);
$documentaryService = $this->getDocumentaryService();
$documentaryService->save($documentary);
}
return $this->render('DocumentaryBundle:Admin:create.html.twig', [
'form' => $form->createView(),
]);
}

Symfony2 DataTransformer after handleRequest

I have an API and I am sending a reference of an entity, I'm using a DataTransformer to get my entity but the DataTransformer is always called before the $form->handleRequest($request) the value is always null and it could't works
My Controller
public function newAction(Request $request)
{
$orderNewFormType = $this->get('competitive_bo.api_bundle.form.type.order_new');
$card = new Card();
try {
$form = $this->createForm($orderNewFormType, $card);
$form->handleRequest($request);
} catch (TransformationFailedException $e) {
return $this->notFoundErrorResponse(
'Business not found'
);
}
if ($form->isValid()) {
return $this->okResponse(array());
}
$validatorErrorFormatter = $this->get('competitive_bo.api_bundle.formatter.validator_error');
$errors = $validatorErrorFormatter->formatFromFormError($form->getErrors(true));
return $this->badRequestErrorResponse(
'Invalid data',
$errors
);
}
The form type
class OrderNewFormType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('customer', 'entity', array(
'class' => 'CompetitiveBOBusinessBundle:Customer',
'property' => 'id'
))
->add('business', 'business', array(
'mapped' => false,
))
;
}
/**
* {#inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
'data_class' => Card::class
));
}
public function getName()
{
return null;
}
}
The Business form type
class BusinessReferenceFormType extends AbstractType
{
/**
* #var ReferenceToBusinessTransformer
*/
private $referenceToBusinessTransformer;
public function __construct(ReferenceToBusinessTransformer $referenceToBusinessTransformer)
{
$this->referenceToBusinessTransformer = $referenceToBusinessTransformer;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer($this->referenceToBusinessTransformer);
}
public function getName()
{
return 'business';
}
public function getParent()
{
return 'text';
}
}
And the DataTransformer
/**
* Class ReferenceToBusinessTransformer
*/
class ReferenceToBusinessTransformer implements DataTransformerInterface
{
/**
* #var BusinessRepository
*/
private $businessRepository;
public function __construct(BusinessRepository $businessRepository)
{
$this->businessRepository = $businessRepository;
}
/**
* {#inheritdoc}
*/
public function transform($reference)
{
var_dump($reference);
$business = $this->businessRepository->findOneBy(array(
'reference' => $reference
));
if (null === $business) {
throw new TransformationFailedException;
}
return $business;
}
/**
* {#inheritdoc}
*/
public function reverseTransform($value)
{
if (!($value instanceof Business)) {
throw new TransformationFailedException;
}
return $value->getReference();
}
}
The var_dump($reference) is always null
And I have my test
public function testNewAction($getParams, $postParam, $responseCode)
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
$route = $router->generate('competitivebo_api_order_new',$getParams);
$client->request('POST', $route, $postParam);
$response = $client->getResponse();
$this->assertJsonResponse($response, $responseCode);
}
With the post params
'customer' => 1,
'business' => LoadBusinessData::REFERENCE_1,
'name' => 'Test',
The exception TransformationFailedException is always thrown during the $this->createForm(...) so the request is not handled
According with the documentation,
When null is passed to the transform() method, your transformer should return an equivalent value of the type it is transforming to (e.g. an empty string, 0 for integers or 0.0 for floats).

Using some dynamic forms related to the same class

Symfony 2.3
I'm embedding some Forms to be able to change each user property related to permissions. I've created an UserAdminType which is displayed for each user in the same page:
<?php
namespace Msalsas\UserAdminBundle\Form;
use Msalsas\UserBundle\Entity\User;
use Msalsas\UserBundle\Entity\ExtendedUser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
class UserAdminType extends AbstractType
{
private $user;
public function __construct(User $user)
{
$this->user = $user;
$this->extendedUser = new ExtendedUser($this->user);
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$extendedUser = $this->extendedUser;
$builder
->add('extendedRole',
'choice', array('choices' => array(
$extendedUser::ROLE_1 => "Role 1",
$extendedUser::ROLE_2 => "Role 2",
$extendedUser::ROLE_3 => "Role 3",
),
'label' => $this->user->getUsername()
))
->add('Change roles for '.$this->user->getUsername(), 'submit')
;
$builder->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
if( ! $form->getClickedButton('Change roles for '.$this->user->getUsername()) )
{
// Here I should avoid submitting the form
}
}
);
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Msalsas\UserBundle\Entity\ExtendedUser',
'empty_data' => function (FormInterface $form) {
return null;
}
));
}
/**
* #return string
*/
public function getName()
{
return 'extendedUserRoleForm';
}
}
The problem is that when I submit one of those forms, all other forms are also submitted, returning an error, because the extendedUser uses a constructor to initialize the object with the User as parameter:
Catchable Fatal Error: Argument 1 passed to Msalsas\UserBundle\Entity\ExtendedUser::__construct() must be an instance of Msalsas\UserBundle\Entity\User, none given, called in .../vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php on line 140 and defined
I've also tried to set the empty_data with a new default ExtendedUser:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Msalsas\UserBundle\Entity\ExtendedUser',
'empty_data' => function (FormInterface $form) {
return $this->extendedUser;
}
));
}
Now, when submitting the form, the new entity is persisted, but the other forms are still submitted, and returning an error: This form should not contain extra fields. This seems to be due to the duplicated property name (extendedRole).
How could I avoid the other forms to be submitted?
I've found out the solution here.
Each Form must have different name. So I've added a $name property, and assigned it in the constructor:
private $user;
private $name = 'default_name_';
private $extendedUser;
public function __construct(User $user, $formName)
{
$this->user = $user;
$this->extendedUser = new ExtendedUser($this->user);
$this->name = $this->name.$formName;
}
//... (No event required)
/**
* #return string
*/
public function getName()
{
return $this->name;
}
The $formName parameter is relative to the current user. In this way, only the "clicked" form is submitted. Wish it helps.

Symfony2 FormType entity field type

I have three entity. Profile, Car and Trip. When user(Profile) create Trip, he can chose the Car(only his own) and assign it to the Trip. I know the field must be the entity type. But I dont know how I can set to chose list a cars of current user(Profile). Any ideas? Thanks.
Filter the cars by user, I think this example is what you need:
$builder->add('car', 'entity', array(
'class' => '/path/to/entity/Car',
'property' => 'title',
'empty_value' => 'Choose a car',
'query_builder' => function(EntityRepository $em) use ($userId) {
return $em->createQueryBuilder('c')
->join('c.user', 'u')
->where('u.id = :userId')
->setParameter('userId', $userId);
}
)
)
You can add $userId as one form option:
$form = $this->createForm(new MyFormType(), $object, array( 'userId' => $userId ));
And inside your form retrieve it:
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'userId' => function (Options $options, $value) {
return $options['userId'];
}
));
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if($options['userId']){
$userId = $options['userId'];
}
}
How I mentioned. other solution, even personally I don't prefer it:
$form = $this->createForm(new MyFormType($userId), $object);
And in your form, store it in a protected variable to be used later in your query:
/**
* Class MyFormType
*/
class MyFormType extends AbstractType
{
protected $userId;
/**
* #param $userId
*/
public function __construct($userId) {
$this->userId = $userId;
}
}
Take a look at this Symfony cookbook entry:
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#how-to-dynamically-generate-forms-based-on-user-data

Categories