I'm making createQueryBuilder inside my BuildForm method to get some records from an entity.
Everything work well, I succeded to display a select with the data I want but I have not a first line with an empty value.
<select id="" name="" class="form-control">
<option value="7">A</option>
<option value="8">B</option>
<option value="9">C</option>
</select>
And here's the code I've a made in the BuildForm method
$builder->add('company', EntityType::class, array(
'class' => Company::class,
'query_builder' => function(EntityRepository $er) use ($options) {
return $er->createQueryBuilder('c')
->where("c.id in {$options['id']}");
},
'label' => 'Company',
));
Just add 'required' => false to the options (where you set the label)
You can set required => false
$builder->add('company', EntityType::class, array(
'class' => Company::class,
// Here you set required to false
'required' => false,
'query_builder' => function(EntityRepository $er) use ($options) {
return $er->createQueryBuilder('c')
->where("c.id in {$options['id']}");
},
'label' => 'Company',
));
Related
In my Form type I have a select choice which list my team entities.
When I do this
$builder->add('teams');
it lists all my teams on my form AND an empty choice, so it's possible to have no team or to remove the team (and that behavior is great for what I have to do).
But when I use a query_builder
$builder->add('teams', EntityType::class, array(
'class' => Team::class,
'query_builder' => function (EntityRepository $er) {
return $er->getTeamsNotRestrictedByAdmin();
},
'choice_label' => '_name'
)
);
it returns the entities I want but I don't have that null choice anymore. How could I get it back properly ?
Go with :
$builder->add('teams', EntityType::class, array(
'class' => Team::class,
'query_builder' => function (EntityRepository $er) {
return $er->getTeamsNotRestrictedByAdmin();
},
'choice_label' => '_name',
'required' => false,
'empty_data' => ''
)
);
I am using sonata admin to set up an admin class with four filters. These filters are multiple choice and related to other entities. I want to make these filters dependent and run one filter query to filter the data considering all selected filter values. So far I just used sonata callback filter, but it can not filter when I select two or three fields to filter data. Is there a technique to override the query sonata run for filters?
$datagridMapper
->add('campaign', null, array(), 'entity', array(
'class' => 'AppPolioDbBundle:Campaign',
'choice_label' => 'campaignName', 'multiple' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->groupBy('u.campaignName');
},
))
->add('region', 'doctrine_orm_callback', array(
'callback' => array($this, 'callbackFilterRegion'),
'field_type' => 'checkbox'
),
'choice',
array('choices' => $this -> getRegionList(), 'multiple' => true))
->add('district.districtCode', null, array(), 'entity', array(
'class' => 'AppPolioDbBundle:District',
'choice_label' => 'districtName', 'multiple' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->groupBy('u.districtName');
},
))
This is code I use to show filed in page.
I need some way to predefine default selected if there is no data.
Any suggestion?
->add('period', EntityType::class, [
'required' => false,
'empty_value' => false,
'expanded' => true,
'class' => 'MainBundle\Entity\NotificationsPeriod',
'query_builder' => function (EntityRepository $er) {
$qb = $er->createQueryBuilder('u');
$qb->orderBy('u.id', 'ASC');
$qb->add('where', $qb->expr()->notIn('u.id', 1));
return $qb;
},
'empty_data' => 2,
])
You're almost there!
You need to set your empty_data parameter as you have to match a value in your HTML.
For example:
//formClass.php
'empty_data' => 2,
Then ..
//template.twig
<select name="whatever">
<option value="2">Something</option>
{# build your options from data.. #}
</select>
The official docs are here: http://symfony.com/doc/current/reference/forms/types/choice.html#empty-data
I am a totally new in symfony2. I met a situation which I can't solve till now.
I have a controller, formtype and eventsubscriber in my project. The form builds by binding the entities. In that case for a particular entity I need to add a default value along with an ID in one of the form fields. My form type is
$builder->add('breed', EntityType::class, array(
'label' => 'Breed',
'class' => 'AppBundle:Masters\Breed',
'placeholder' => '----Select Breed----',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('b')
->orderBy('b.sortOrder', 'DESC');
},
'choice_label' => 'breed',
));
$builder->addEventSubscriber(new BreedSubscriber($factory));
My Event Subscriber is
private function addBreed($form, $breedmasterId) {
$form->add($this->factory->createNamed('breed',EntityType::class,null, array(
'class' => 'AppBundle:Masters\Breed',
'placeholder' => '----Select Breed--------',
'choice_label' => 'breed',
'required' => TRUE,
'mapped' => false,
'query_builder' => function (EntityRepository $repository) use ($breedmasterId) {
$qb = $repository->createQueryBuilder('bm')
->where('bm.breed = :breedmasterId')
->setParameter('breedmasterId', $breedmasterId);
return $qb;
},
'auto_initialize' => false
)));
}
I need to add a default value such as "General" along with an "id" in the addBreed subscriber and it need to be validate on formsubmission. Thanks in advance for the help.
You can add the following 'data' key in the array you configure your form:
$builder->add('breed',null, array(
'label' => 'Breed',
'data' => 'Sih Tzu'
))
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?
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.