Advertisement
Guest User

ChoicesWithPredefinedCollection

a guest
Dec 5th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. // within the controller it works like this:
  2.  
  3. $defaultData = ['books' => $booksVal];
  4.  
  5.        $myForm = $this->createFormBuilder($defaultData)
  6.             ->add('books', ChoiceType::class, [
  7.                  'choices' => $defaultData,
  8.                 'choice_label' => function ($value) { return $value; },
  9.  
  10. // however, integrated in a CustomType it doesn't:
  11.  
  12. $form = $this->createForm(
  13.             /* type */ CustomeType::class,
  14.             /* data */ ['books' =>  $booksVal]);
  15.  
  16. // here is the CustomType:
  17.  
  18.  public function buildForm(FormBuilderInterface $builder, array $options)
  19.     {
  20.  
  21.  
  22.         $builder
  23.             ->add('books', ChoiceType::class, [
  24.                'choices' => /* what I'm supposed to write here? */ ,
  25.                 'choice_label' => function ($value) { return $value; },
  26.                 'multiple' => true,
  27.                 'expanded' => true
  28.  
  29. // The Symfony docu says, but how do I get $group within my CustomType?
  30. // @link: https://symfony.com/doc/master/reference/forms/types/entity.html ('Using Choices')
  31.  
  32. $builder->add('users', EntityType::class, array(
  33.     'class' => 'AppBundle:User',
  34.     'choices' => $group->getUsers(), // how can this be achieved?
  35. ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement