Symfony 2 Form: how to bind data - symfony

How to bind data which is gained by using call:
$attributes = $em->getRepository('\OBB\Entity\Attribute')->findAllWithAllRelations($id);
to a Symfony 2 Form
Because according to a manual you need to have a method defined in Entity which is bound to a form.

You should add a form type for editing an individual attribute. This could look something like:
namespace OBB\Form;
class AttributeType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'OBB\Entity\Attribute',
);
}
public function getName()
{
return 'obb_attribute';
}
}
Then you can use a collection form to edit a collection of them simultaneously.
$form = $this->createForm('collection', $attributes, array(
'type' => new AttributeType(),
));

Related

Use query_builder on CollectionType in symfony4 forms?

In a symfony 4 form, I need to use something like a query_builder option that is available on EntityType but from a CollectionType. There is a similar question here with no good answers.
In my project, each Site entity has many Goal. Each Goal has a numeric goal and a specific date. I'd like to edit the goals of a site for a specific date only. The problem is that a CollectionType form pulls all goals to show in the form, but I only want to pull the goals for a given date. How? There is no query_builder on a CollectionType like there is on an EntityType. I could change the getter in my Site entity, but I don't know how to pass the needed date to my getter.
For now my work-around is to render the entire form (with ALL associated goals for a given site), and then use some javascript to hide all goals except those with the date to edit. This works, but it's a terrible solution for sites with lots of goals spanning a range of dates.
My Site entity (only relevant code is shown):
class Site
{
public function __construct()
{
$this->goals = new ArrayCollection();
}
/** #ORM\OneToMany(targetEntity="App\Entity\Goal", mappedBy="site") */
private $goals;
public function getGoals()
{
return $this->goals;
}
}
and my related Goal entity:
class Goal
{
/** #ORM\Column(type="date") */
private $goalDate;
/** #ORM\Column(type="integer") */
private $goal;
/** #ORM\ManyToOne(targetEntity="App\Entity\Site", inversedBy="goals") */
private $site;
// ...
}
My forms:
class SiteGoalsAdminForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('goals', CollectionType::class, [
'entry_type' => GoalsEmbeddedForm::class,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Site::class
]);
}
}
and the individual goal form:
class GoalsEmbeddedForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('goal', IntegerType::class)
->add('goalDate', DateType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Goal::class,
]);
}
}
Using Form Events, while avoiding the allow_add and allow_delete options for the CollectionType form might land you in the right neighbourhood:
First - let's assume we're filtering by year, for ease of example, and that the year is being scooped up from a ?y=2018 style of querystring. We'll pass that info down to the form builder:
<?php
// Inside a *Action method of a controller
public function index(Request $request): Response
{
// ...
$filteredYear = $request->get('y');
$form = $this->createForm(SiteGoalsAdminForm::class, $site, ['year_filter' => $filteredYear]);
// ...
}
This implies we should be updating the default options for the SiteGoalsAdminForm class:
<?php
// SiteGoalsAdminForm.php
// ...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Site::class,
'year_filter' => 2018
]);
}
// ...
Then, in the buildForm method of that same class, we could access the Site object and remove Goals from it where the year of the goalDate did not fall inside the form's
<?php
// SiteGoalsAdminForm.php
namespace App\Form;
// ... other `use` statements, plus:
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class SiteGoalsAdminForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($options) {
$form = $event->getForm();
/** #var Site */
$site = $event->getData();
$goals = $site->getGoals();
foreach ($goals as $g) {
if ($g->getGoalDate()->format('Y') !== (string) $options['year_filter']) {
$site->removeGoal($g);
}
}
$form->add('goals', CollectionType::class, [
'entry_type' => GoalsEmbeddedForm::class,
]);
}
);
}
// ...
}
Not a query_builder exactly, but functionally similar.
Filter the results using the entity manager in the controller that you want to set on the collection type.
$goals = $entityManager->getRepository(Goals::class)->findBy(['year' => 2020]);
$form = $this->createForm(SiteGoalsType::class, $site, [
'goals' => $goals
]);
Then configure the SiteGoalsType::class to accept new option goals.
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Site::class,
]);
$resolver->setRequired(['goals']);
}
In the buildForm method of SiteGoalsType::class Set the data to the collection type field from the options.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('goals', Type\CollectionType::class, [
'entry_type' => GoalsEmbeddedType::class,
'data' => $options['goals'],
'mapped` => false
]);
}
Make sure the add the 'mapped' => false to your collection type field else it may lead to removing the records that didn't falls in the filter we have written in the controller.
$goals = $entityManager->getRepository(Goals::class)->findBy(['year' => 2020]);

Symfony 3 - subform and element name

I want to create form with composition pattern like this:
https://symfony.com/doc/current/form/inherit_data_option.html
I use Symfony 3.
and it's working. I have each element like single object and add this.
but finally my form elements names have name like
form[subform][element]
How to make flat structure without subform in name attribute?
use AppBundle\Base\Form\NickType;
use AppBundle\Base\Form\MailType;
use AppBundle\Base\Form\PassType;
use AppBundle\Base\Form\UserType;
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', NickType::class)
->add('mail', MailType::class)
->add('password', PassType::class)
->add('repeat_password', PassType::class)
(etc...)
and SINGLE ELEMENT
class NickType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'inherit_data' => true
));
}
}
You don't need to define a NickType if it only inherits a TextType. You can remove NickType, MailType, etc.
You can just do:
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', TextType::class)
;
(etc...)
If you want to reuse a form field, you have to create a Custom Form Field Type:
class NickType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
//...
));
}
public function getParent()
{
return TextType::class;
}
}
You can remove form[] from the element name, but removing this is not really recommended, because when you read the request to populate form data you can identify the form by its form name. (via)
You can set the name of the root form to empty, then your field name
will be just form. Do so via
// the first argument to createNamedBuilder() is the name
$form = $this->get('form.factory')->createNamedBuilder(null, 'form', $defaultData)
->add('from', 'date', array(
'required' => false,
'widget' => 'single_text',
'format' => 'dd.MM.yyyy'
));
(via)

Why does data_class leads to LogicException?

I have a form type (field_type) which extends text and have a data_class. Passing an entity instance to the form via event listener leads to a LogicException:
The form's view data is expected to be an instance of class Entity,
but is a(n) string. You can avoid this error by setting the
"data_class" option to null or by adding a view transformer that
transforms a(n) string to an instance of Entity.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('field', 'field_type', $opts);
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use($options)
{
...
$form = $event->getForm();
$form->get('field')->setData($entity);
});
}
$entity is an instance of the data_class. The form type has a view data transformer, too.
Field type:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer($this->viewTransformer, true);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Entity',
'invalid_message' => 'The given id is invalid!',
'required' => true
));
}
public function getParent()
{
return 'text';
}
Everything works fine except the part of the data_class. If I remove the data_class it works.
Why do I need to remove this part?

how to set field value for collection type in symfony2

//--form timesheettype---it is not entity class
class TimeSheetType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options) {
$builder->
add('dailyTimeSheet', 'collection', array('type' => new DailyType(), 'allow_add' => true, 'allow_delete' => true, 'prototype' => true,))
->add('comment','textarea');
}
public function getName() {
return 'TimeSheetDaily';
}
}
//--- DailyType -- there is entity for this type
class DailyType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options) {
$builder->add('project','entity',array('class'=> 'Parabola\EntityBundle\Entity\Project','property'=>'name'))
->add('projectTask', 'entity', array('class'=> 'Parabola\EntityBundle\Entity\ProjectTask','property'=>'name'))
->add('hours', 'text');
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Parabola\EntityBundle\Entity\TimeSheetDaily');
}
public function getName() {
return 'DailySheet';
}
//-- controller--
$repository = $this->getDoctrine()
->getRepository('ParabolaEntityBundle:TimeSheetDaily')->findAll();
$form = $this->createForm(new \Parabola\TimeSheetBundle\Form\TimeSheetType(),$repository);
I have entity class TimeSheetDaily. While building form of TimeSheetType, I am passing array of TimeSheetDaily class object to form type. and that TimeSheetType has collection of DailyType. It is not Setting value to the collection field which is nothing but a TimeSheetDaily entity.
Have you defined __constructor of TimeSheetDaily class?
Do you have something like this?
public function __construct(){
//....
$this->dailyTimeSheet = new ArrayCollection();
//....
};
It's important to correctly initialize this collection in order for Symfony to be able to insert data into it....

Is it possible to have collection field in Symfony2 form with different choices?

I have a collection field with elements of type choice in my Symfony form. Each element should have different list o choices. How can I arrange this in Symfony2? I can't use choices option because every element will have the same choices. I have seen the choice_list option which takes an object that can produce the list of options, but I don't see how it could produce a different choices for different elements in collection.
Any idea how to deal with that?
I think you need form event : http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html.
To change the default way the collection is made.
The main form is simple:
namespace Acme\Bundle\AcmeBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Acme\Bundle\AcmeBundle\Form\DescriptorDumpFieldsType;
class TranscodingType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('descriptorDumpFields', 'collection', array('type' => new DescriptorDumpFieldsType()));
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\Bundle\AcmeBundle\Entity\Descriptor',
);
}
public function getName()
{
return 'descriptor';
}
}
Just a simple form with a collection of sub forms.
The second one use a form subscriber who handle the form creation. (using form events)
So the first form is created normaly and add many DescriptorDumpFieldsType wich are dynamicly created.
namespace Acme\Bundle\AcmeBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormTypeInterface;
use Acme\Bundle\AcmeBundle\Form\EventListener\TranscodingSubscriber;
class DescriptorDumpFieldsType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$subscriber = new TranscodingSubscriber($builder->getFormFactory());
$builder->addEventSubscriber($subscriber);
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\Bundle\AcmeBundle\Entity\DescriptorDumpField',
);
}
public function getName()
{
return 'desc_dump_field';
}
}
The form subscriber :
namespace Acme\Bundle\AcmeBundle\Form\EventListener;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Acme\Bundle\AcmeBundle\Entity\DumpField;
use Acme\Bundle\AcmeBundle\Form\Transcoding\DataTransformer\JsonToHumanDateTransformer;
class TranscodingSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
return array(FormEvents::SET_DATA => 'setData');
}
public function setData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (!is_null($data)) {
$this->buildForm($data, $form);
}
}
protected function buildForm($data, $form)
{
switch ($data->getDumpField()->getType()) {
case DumpField::TYPE_ENUM:
$type = 'enum'.ucfirst($data->getDumpField()->getKey());
$class = 'dump_field_'.strtolower($data->getDumpField()->getKey());
$form->add($this->factory->createNamed('collection', 'transcodings', null, array('required' => false, 'type' => $type, 'label' => $data->getDumpField()->getKey(), 'attr' => array('class' => $class))));
break;
case DumpField::TYPE_DATE:
$transformer = new JsonToHumanDateTransformer();
$class = 'dump_field_'.strtolower($data->getDumpField()->getKey());
$builder = $this->factory->createNamedBuilder('human_date', 'params', null, array('label' => $data->getDumpField()->getKey(), 'attr' => array('class' => $class)));
$builder->prependNormTransformer($transformer);
$form->add($builder->getForm());
break;
}
}
}
So you can customize the way you want, each sub-form of the collection in buildForm.

Resources