1

Starting with Symfony is quite a learning curve. Even after reading for hours, I cannot get across this presumably simple problem. I want to load a choices-form with values from an entity.

Controller:

namespace AppBundle\Controller
class ItemController extends Controller
{ 
  public function itemAction (Request $request)
    {
      $myItems = new Itemlist();
      //some statements to fill $myItems
      $form = $this->createForm (AllitemsType::class, $myItems);
      // some more stuff
      return $this->render (...);
    }
}

Entity:

namespace AppBundle\Entity;
class Itemlist 
{
  protected $choices;
  protected $defaultvalue;

  public function __construct ()
  {
    $choices = array();
  }

  // all the get and set-methods to fill/read the $choices array and $defaultvalue
}

Form:

namespace AppBundle\Form
class AllitemsType extends AbstractType
{
  public function buildForm (FormBuilderInterface $builder, array $options)
  {
    // and here is my problem: how can I fill next two lines with values from the Itemlist-Entity?
    // The Itemlist instance has been build in the controller and is unknown here
    $items = ??? // should be 'AppBundle\Entity\Itemlist->$choices
    $defaultitem = ??? // should be 'AppBundle\Entity\Itemlist->$defaultvalue

    $choices_of_items = array (
      'choices' => $items,
      'expanded' => true,
      'multiple' => false,
      'data' => $defaultitem,
    );

    $builder->add ('radio1', ChoiceType::class, $choices_of_items);

  }
}

Any help appreciated, Wolfram

2 Answers 2

2
$builder->add('choices', ChoiceType::class);

should be sufficient as you're binding an entity to the form, the process of getting values and setting them back is automatic. Of course you need to have setter and getter for choices field in AllitemsType

To give a complete answer - part above is the so called "best practice one" - you can also choose one of the following

$items = $options['data'];

or

$builder->addEventListener(
  FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    $allItems = $event->getData();
    $form = $event->getForm();
    $form->add('radio1', ChoiceType::class, [
      'choices' => $allItems
    ]);
});

Second one should be preferred as, in options['data'], entity could change during form event's lifetime.

Sign up to request clarification or add additional context in comments.

2 Comments

your "best practice" answer does solve to fill choices, but does not respect to fill the 'data' attribute, correct? And sorry, I do not grab the other two optional answers. Anyway thanks for contributing.
@wolfram sorry, I forgot data option but is the same method i use for choices
0

Pass variables with createForm object.

Controller:

    namespace AppBundle\Controller
    class ItemController extends Controller
    { 
      public function itemAction (Request $request)
        {
          $myItems = new Itemlist();
          $formVars = array("items" => array(1,2,3,4,6), "defaultItems" => 2); // Store variables
          ^^

          //some statements to fill $myItems
          $form = $this->createForm (new AllitemsType($formVars), $myItems);
                                                      ^^
          // some more stuff
          return $this->render (...);
        }
    }

Now create constructor in form and set class variables items and defaultitem in form.

Form:

namespace AppBundle\Form
class AllitemsType extends AbstractType
{

  $this->items = array();
  $this->defaultitem = 0;

  public function __construct($itemArr)
  {
    $this->items = $itemArr['items'];
    $this->defaultitem = $itemArr['defaultItems'];
  }

  public function buildForm (FormBuilderInterface $builder, array $options)
  {
    $choices_of_items = array (
      'choices' => $this->items, // User class variable
      'expanded' => true,
      'multiple' => false,
      'data' => $this->defaultitem, // User class variable
    );

    $builder->add ('radio1', ChoiceType::class, $choices_of_items);

  }
}

It should solve your problem.

3 Comments

Thanks a lot. This is exactly, what I was looking for.
Don't do this. new FormType() has been removed in Symfony 3.x. Pass additional information via the options parameter.
@Cerad seems like Wolfram is using Symfony 2.x version as per his provided code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.