vellmur
vellmur

Reputation: 272

Symfony form builder default select by EntityType

I try to create form with html select element using EntityType. I must get values by some condition and by this condition necessary default value not select from database. So i get all options values, without one, that must be a default value. So i try to find a way to put this value to select. What i tried...

Set value in form:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $growing, ['user_id' => $user_id]);

$tableForm->get('garden')->setData($garden);
$form = $tableForm->createView()

Then i tried to set data in entity:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$growing->setGarden($garden);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $growing, ['user_id' => $user_id]);
$form = $tableForm->createView()

Then i tried to set default select value in form_builder using 'data' attribute:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $grow, [
            'user_id' => $user_id,
            'selected_choice' => $garden
        ]);
$form = $tableForm->createView();

Form_builder code:

class GrowingType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('garden', EntityType::class , [
                'class' => 'FarmBundle\Entity\Garden',
                'query_builder' => function (GardenRepository $gr) use ($options) {
                    return $gr->queryFreeGardens($options['user_id']);
                },
                'attr' => [
                    'data-type' => 'text',
                    'class' => 'table-select',
                    'disabled' => true
                ],
                'required' => false,
                'data' => $options['selected_choice']
            ]);
     }

     /**
      * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FarmBundle\Entity\Growing',
            'selected_choice' => null,
            'user_id' => null
        ));
    }
 }

And code of query for query builder:

class GardenRepository extends \Doctrine\ORM\EntityRepository
{
    public function queryFreeGardens($user_id)
    {
        $qb = $this->createQueryBuilder('g')
            ->leftJoin('g.growing', 'grow')
            ->where('grow.plantDate is NULL')
            ->orWhere('grow.endDate is not NULL')
            ->andWhere('g.user = :user_id')
            ->orderBy('g.name')
           ->setParameter('user_id', $user_id);

    return $qb;
    }
}

And all of this 3 methods not works. Result is one, if entity not get for query in query builder, i cant set this entity. If i will set entity as default value, that was in query builder all will works fine.
How can i solve this problem?

Upvotes: 3

Views: 2384

Answers (1)

Denis Alimov
Denis Alimov

Reputation: 2901

try this in controller:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $grow, [
            'user_id' => $user_id,
            'additional_id' => 7
        ]);
$form = $tableForm->createView();

in form:

class GrowingType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('garden', EntityType::class , [
                'class' => 'FarmBundle\Entity\Garden',
                'query_builder' => function (GardenRepository $gr) use ($options) {
                    return $gr->queryFreeGardens($options['user_id'], $options['additional_id');
                },
                'attr' => [
                    'data-type' => 'text',
                    'class' => 'table-select',
                    'disabled' => true
                ],
                'required' => false
            ]);
     }

     /**
      * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FarmBundle\Entity\Growing',
            'additional_id' => null,
            'user_id' => null
        ));
    }
 }

in repository:

class GardenRepository extends \Doctrine\ORM\EntityRepository
{
    public function queryFreeGardens($user_id, $additional_id)
    {
        $qb = $this->createQueryBuilder('g')
            ->leftJoin('g.growing', 'grow')
            ->where('grow.plantDate is NULL')
            ->andWhere('g.id = :additional_id')
            ->orWhere('grow.endDate is not NULL')
            ->andWhere('g.user = :user_id')
            ->andWhere('g.id = :additional_id')  
            ->orderBy('g.name')
           ->setParameter('user_id', $user_id)->setParameter('additional_id', $additional_id);

    return $qb;
    }
}

maybe you will need to adjust your repository method to retrieve values in right way. There are or clause, you should add this additional id to both branches of your or clause. The main idea is to retrieve you selected object too.

Upvotes: 1

Related Questions