Form mapped to class by different name - symfony

I would like to associate my class with the forms. Is it possible that I gave different names for forms than they are in the class? For example class have property username but I would like to be mapped in the form to input of id login
use Symfony\Component\Form\FormBuilderInterface;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('login', 'text', array (
'label' => 'Login'
)) //<- different name mapped by login
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User',
));
}
User entity:
class User {
protected username;
//...
}

Yes. You are looking for property_path. See http://symfony.com/doc/2.8/reference/forms/types/form.html#property-path
$builder->add('login', 'text', array(
'label' => 'Login',
'property_path' => 'username',
));

Yes you can. Add 'mapped' => false option to the field which you want to have different name than it is in entity. This tells symfony that when the form will be submitted, it would not try to map that field to entity. But here, you will need to do additional work - 1. add data from entities username property to the unmapped one, and 2. when submitting the form, map manually submitted login data to the username property. These steps can be done via Form Events, like this:
use Symfony\Component\Form\FormBuilderInterface;
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Set other fields...
$builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
}
function onPreSetData(FormEvent $event)
{
$form = $event->getForm(); // The form
$data = $event->getData(); // It will be User entity
$form
->add('login', 'text', array (
'label' => 'Login',
'mapped' => false,
'data' => $data->getUsername(),
));
}
function onPostSubmit(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
$data->setUsername($form->get('login')->getData());
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User',
));
}
I'm not sure why you want to do this, but I showed you one way of doing this. Another and simplier way like qooplmao commented would be using property_path.

Related

Symfony 5 : how to add data to a user authenticated in a session, using OneToMany relation?

I want to add data to the strategy table via the title field, and relate it to the user authenticated in a session with the user_id foreign key.
The code below adds data to the strategy table with the relation working, but via a select option (choice_label) coded in my FormType file, listing all the users in my view.
I want to replace that select option by a code which gets the user authenticated in the session instead.
I looked into the Security and Session parts of the documentation, but couldn't make it work.
My tables :Database
My Controller file:
public function create(Strategy $strategy = null, Request $request, EntityManagerInterface $em)
{
$strategy = new Strategy();
$form = $this->createForm(StrategyType::class, $strategy);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$em->persist($strategy);
$em->flush();
return $this->redirectToRoute("frStrategy");
}
return $this->render('strategy/updateStrategy.html.twig', [
"strategy" => $strategy,
"form" => $form->createView()
]);
My FormType file:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('user', EntityType::class,[
'class' => User::class,
'choice_label' => 'username'
])
;
}
Either pass the current user to form in an option or inject the security component in the form and use it from there. I think it kinda weird to put a select if you know you'll always have only one option in it but that's another subject.
private $security;
public function __construct(Security $security)
{
$this->security = $security
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('user', EntityType::class,[
'class' => User::class,
'choice_label' => 'username',
'choices' => [$this->security->getUser()],
])
;
}

symfony3 - Showing 'This value is not valid' for choice type of dropdown that options adding via ajax

My scenario is,
I need to create a entity(module) with display order and the module is under a topic entity and association made correctly. On form load the topic dropdown and display order dropdown will be blank along the module name. When selecting topic the display order will fill with options via ajax/js. Display order will be 1 to a number that will be the total modules under the specific topic+1 . The upcoming display order will be selected automatically. And that's working perfectly. But my issue is about the display order validation after submit. Its saying 'This value is not valid'. I understands this is due to not giving 'choices' as array in form type, but this case i cant give as static in form type. Please help anyone knows a solution.
class ModuleType extends AbstractType {
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('topic', EntityType::class, [
'class' => 'AppBundle:Topic',
'choice_label' => 'name',
'placeholder' => 'Choose a Topic'
])
->add('name')
->add('description', TextareaType::class)
->add('displayOrder', ChoiceType::class)
->add('save', SubmitType::class, [
'attr' => ['class' => 'form-control button btn-sm nomargin']
])
;
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Module'
));
}
}
Try to use the Event Listener.
In your case for exemple:
// TopicType.php
private function addEventListener(FormBuilderInterface $builder)
{
// this function permit to valid values of topics
$annonymFunction = function(FormInterface $form, $diplayOrder) {
$entities = $this->container->get('doctrine.orm.default_entity_manager')
->getRepository('YourBundle:Topic')
->findAll();
if ($entities) {
$topics = array();
foreach($topics as $topic) {
$topics[$topic->getName()] = $topic->getName();
}
}
else $topics = null;
$form->add('topic', EntityType::class, array(
'attr' => array('class' => 'topic'),
'choices' => $topics));
};
$builder
->get('displayOrder')
->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($annonymFunction) {
$annonymFunction($event->getForm()->getParent(), $event->getForm()->getData());
});
}
Hope to help.

Symfony2: formbuilder : dynamically modify querybuilder

I am using the formbuilder to create a form as followed:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', 'textarea')
->add('rosters', 'entity', array(
'class' => 'PlatformBundle:team',
'property' => 'display',
'multiple' => true,
'expanded' => true,
'required' => true
))
->add('send', 'submit')
;
}
At the moment I get all "teams". I need to adapt the form to display certain teams depending of the request.
I can use the query-builder inside the form builder
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', 'textarea')
->add('rosters', 'entity', array(
'class' => 'PlatformBundle:team',
'property' => 'display',
'query_builder' => function(TeamRepository $t) use ($userId) {
return $r->createQueryBuilder('t')
->where('(t.user = :user')
},
'multiple' => true,
'expanded' => true,
'required' => true
))
->add('send', 'submit')
;
}
But the query changes for different questionnaire. In brief: always the same questionnaire but different teams to be listed (Am I making sense?).
Does someone has an idea how dynamically modify the querybuilder inside a formbuilder?
I suggest two possible alternatives.
If the request comes from the form itself (i.e. you already submitted the form with some data and want to refine the fields) you can access the submitted data like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$data = $builder->getData();
// now you can access form data
If the request comes from another source, you should use the "options" parameter. First, build a new $option for the requested user:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'user' => null,
));
}
Note: I set the default to null but you can set it to whatever you want.
After that you can pass the $option where you build the form, i.e.
// some controller
$option = array('user' => $request->get('user');
$teamForm = $this->createForm(new TeamType(), null, $options);
// ...
For those looking for an answer...
The best solution I found is create a variable in the formtype and to import it form the controller. My formType will look like that:
class formType extends AbstractType
{
// declare and construct the query in the class to use it in the function
private $qb;
public function __construct ($qb)
{
$this->qb = $qb;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// declare the variable within the function
$qb = $this->qb;
$builder
->add('content', 'textarea', array('required' => false))
->add('rosters', 'entity', array(
'class' => 'PlatformBundle:Team',
// use ($qb) -> $qb is query built in the controller (or repository)
'query_builder' => function(TeamRepository $r) use ($qb) {
return $qb;
},
'property' => 'display',
'multiple' => true,
'expanded' => true,
'required' => true
))
->add('send', 'submit');
}
In my controller I just pass $qb as an argument of the formtype
$qb = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Team')->qbteam($Id);
$form = $this->createForm(new formType($qb), $form);
with qbteam a function in the team repository which return the query (not a result).
public function qbteam($Id){
$qb = $this->createQueryBuilder('r')
->leftJoin('r.team', 'm')
->addSelect('m')
->where('m.user = :user')
->setParameter('user', $Id);
return $qb;
}
I hope it will help others.
cheers

Calling $builder->getData() from within a nested form always returns NULL

I'm trying to get data stored in a nested form but when calling $builder->getData() I'm always getting NULL.
Does anyone knows what how one should get the data inside a nested form?
Here's the ParentFormType.php:
class ParentFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('files', 'collection', array(
'type' => new FileType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
);
}
}
FileType.php
class FileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Each one of bellow calls returns NULL
print_r($builder->getData());
print_r($builder->getForm()->getData());
die();
$builder->add('file', 'file', array(
'required' => false,
'file_path' => 'file',
'label' => 'Select a file to be uploaded',
'constraints' => array(
new File(array(
'maxSize' => '1024k',
))
))
);
}
public function setDefaultOptions( \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver )
{
return $resolver->setDefaults( array() );
}
public function getName()
{
return 'FileType';
}
}
Thanks!
You need to use the FormEvents::POST_SET_DATA to get the form object :
$builder->addEventListener(FormEvents::POST_SET_DATA, function ($event) {
$builder = $event->getForm(); // The FormBuilder
$entity = $event->getData(); // The Form Object
// Do whatever you want here!
});
It's a (very annoying..) known issue:
https://github.com/symfony/symfony/issues/5694
Since it works fine for simple form but not for compound form. From documentation (see http://symfony.com/doc/master/form/dynamic_form_modification.html), you must do:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$product = $event->getData();
$form = $event->getForm();
// check if the Product object is "new"
// If no data is passed to the form, the data is "null".
// This should be considered a new "Product"
if (!$product || null === $product->getId()) {
$form->add('name', TextType::class);
}
});
The form is built before data is bound (that is, the bound data is not available at the time that AbstractType::buildForm() is called)
If you want to dynamically build your form based on the bound data, you'll need to use events
http://symfony.com/doc/2.3/cookbook/form/dynamic_form_modification.html

passing data from controller to Type symfony2

if i show a field of type "entity" in my form, and i want to filter this entity type based on a argument I pass from the controller, how do i do that.. ?
//PlumeOptionsType.php
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('framePlume', 'entity', array(
'class' => 'DessinPlumeBundle:PhysicalPlume',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('pp')
->where("pp.profile = :profile")
->orderBy('pp.index', 'ASC')
->setParameter('profile', ????)
;
},
));
}
public function getName()
{
return 'plumeOptions';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Dessin\PlumeBundle\Entity\PlumeOptions',
'csrf_protection' => true,
'csrf_field_name' => '_token',
// a unique key to help generate the secret token
'intention' => 'plumeOptions_item',
);
}
}
and inside the controller, i create the form :
i have that argument that i need to pass in my action code:
$profile_id = $this->getRequest()->getSession()->get('profile_id');
...
and then i create my form like this
$form = $this->createForm(new PlumeOptionsType(), $plumeOptions);
the $plumeOptions is just a class to persist. But it has a one-to-one relationship with another class called PhysicalPlume. Now, when i want to display the 'framePlume' in my code, i want to show a filtered PhysicalPlume entity.
You can pass parameters to the form class as follows:
//PlumeOptionsType.php
protected $profile;
public function __construct (Profile $profile)
{
$this->profile = $profile;
}
Then use it in the query_builder of your buildForm:
$profile = $this->profile;
$builder->add('framePlume', 'entity', array(
'class' => 'DessinPlumeBundle:PhysicalPlume',
'query_builder' => function(EntityRepository $er) use ($profile) {
return $er->createQueryBuilder('pp')
->where("pp.profile = :profile")
->orderBy('pp.index', 'ASC')
->setParameter('profile', $profile)
;
},
));
And finally in your controller:
// fetch $profile from DB
$form = $this->createForm(new PlumeOptionsType($profile), $plumeOptions);
You can use $plumeOptions to pass everything your argument, but you'll need to add a getDefaultOptions() in PlumeOptionsType to specify the default value for your option.
See for instance https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php to see what this method should look like.

Resources