I have a form where the dropdown value is coming from another entity.
->add('country', 'entity', array(
'label' => 'Country',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.countryName', 'ASC');
},
'empty_value' => 'Select Country',
'required' => true,
'mapped' => true,
'class' => 'BundleAdminBundle:KidsKulaCountry',
'attr' => array('class' => 'form-control sml-frm'),
//...
))
In the twig file i want to set selected the value at edit.
{{ form_widget(form.country, {value: Setcountry } ) }}
if I print
{{Setcountry}}
its returns result but it does not set selected.Please help me out.
I suppose that your Setcountry is the id of the entity you want to select. You can do this with the conversion of the Setcountry of int to string in the next way:
{{ form_widget(form.country, {value: Setcountry ~ "" } ) }}
Related
I have a form with a list of checkboxes and I would like to display the archived services with the "text-muted" class (which displays the text more clearly).
With the code below, there is no error but the class is not applied.
$builder->add('prestationTypes', EntityType::class, [
'class' => 'App\Entity\PrestationType',
'choice_label' => function ($value, $key, $index) {
return $value->getLibelle() . ' ' . ($value->getIsArchived() ? '(archivée)' : '');
},
'multiple' => true,
'expanded' => true,
'required' => false,
'by_reference' => false,
'placeholder' => 'Choisissez...',
'query_builder' => function (EntityRepository $er) use ($autoecole) {
return $er->getPTByCategorie($autoecole, 'planning');
},
'choice_attr' => function ($value, $key, $index) {
return $value->getIsArchived() ? ['class' => 'text-muted'] : ['class' => ''];
},
]);
It's displayed like this in my view.
<div class="form-group">
{{ form_label(form.prestationTypes, 'Types de tickets rattachés') }}
{{ form_widget(form.prestationTypes) }}
</div>
And the current result (without the archived lines in more clear).
liste
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' => ''
)
);
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'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.
I have a form which contains an array of entites, rendered as checkboxes:
$form = $this->createForm(new BasicListType(), $lists);
class BasicListType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('lists', 'entity', [
'required' => false,
'class' => 'MyBundle:ListEntity',
'property' => 'id',
'property_path' => '[id]',
'multiple' => true,
'expanded' => true,
]);
}
public function getName()
{
return 'list';
}
}
When I display my form, the rows are not in the same order as the $lists I built it with.
{{ form_start(form) }}
{% for list in form.lists %}
{{ form_row(list) }}
{% endfor %}
{{ form_end(form) }}
In fact, they're alphabetised by the id property. Why is this happening and more importantly, is there any way I can stop it?
Probably you need to use the query_builder option to construct an custom query, and order the result as you want. Should look something like that:
$builder->add('lists', 'entity', [
'required' => false,
'class' => 'MyBundle:ListEntity',
'property' => 'id',
'query_builder' => function(EntityRepository $er){
$qb = $er->createQueryBuilder('l');
$qb
->select('l')
->orderBy('DESIRED ORDER FIELD', 'ASC OR DESC') // change this!!
;
return $qb;
}
'property_path' => '[id]',
'multiple' => true,
'expanded' => true,
]);
You can pass an QueryBuilder Object directly too. Take a look of the docs.