nielsv
nielsv

Reputation: 6800

Set selected value on entity field (Symfony Forms)

I'm working with the Sonata AdminBundle and I would like to set the selected value of my select list.

I have a Category with fields: CategoryID, parentID, tag. The parentID can be NULL.

The code how I build my form:

// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{        
    // If edit -> get tag and id from tag
    if($this->subject->getCategoryId() !== null)
    {
        $tag_current = $this->subject->getCategoryId()->getTag();
        $tag_current_id = $this->subject->getCategoryId()->getParentid()->getCategoryid();
    }

    $formMapper
        ->add('tag', 'text', array('label' => 'Tag'))
        ->add('name', 'text', array('label' => 'Naam'))
        ->add('nameEN', 'text', array('label' => 'Naam Engels', 'required' => false, 'data' => 'test'))
        ->add('parentcategory', 'entity', array(
            'class' => 'DX\MyBundle\Entity\Category',
            'empty_data'  => null,
            'empty_value' => "GEEN PARENT CATEGORIE",
            'required' => false,
            'data' => $tag_current_id,
            'query_builder' => function(EntityRepository $er) use ($tag_current) {

                return $er->createQueryBuilder('c')
                    ->where('c.parentid IS NULL')
                    ->andWhere('c.tag != :tag')
                    ->andWhere('c.tag != :tag_current')
                    ->setParameter('tag', 'FOTOGRAAF')
                    ->setParameter('tag_current', $tag_current);
            }
        ))
    ;
}

As you see I've tried to set data property with no result. The empty value NO PARENT CATEGORY is always selected on page load.

My $tag_current_id is an integer, in this case: 11 . And my select list looks like this:

<select id="s54ff22c20ca39_parentcategory" name="s54ff22c20ca39[parentcategory]" class="select2-offscreen" tabindex="-1" title="Parentcategory">
    <option value="">GEEN PARENT CATEGORIE</option>
    <option value="1">THEMA</option>
    <option value="11">FREEM SELECTIE</option>
</select>

But still not selected. What am I doing wrong?

Upvotes: 2

Views: 1524

Answers (1)

HypeR
HypeR

Reputation: 2206

Your query builder should return all the available values, it will select automatically your database value :

$formMapper->add('parentcategory', 'entity', array(
    'class' => 'DX\MyBundle\Entity\Category',
    'empty_data'  => null,
    'empty_value' => "GEEN PARENT CATEGORIE",
    'required' => false,
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('c')
                  ->where('c.parentid IS NULL')
                  ->andWhere('c.tag != :tag')
                  ->setParameter('tag', 'FOTOGRAAF')
    }
))

Update:

This is what i'm doing in my project :

$formMapper->add('supplier', 'entity', array(
    'class' => 'AcmeCoreBundle:ShopSupplier',
    'empty_value' => 'None',
    'empty_data' => null,
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('ss')
            ->orderBy('ss.name', 'ASC');
    }
))

It selects automatically the database value and set the empty_value when none is selected. If it doesn't work in your project, i have no idea.

Upvotes: 1

Related Questions