I'm using Symfony3 form. I have aCountryType in my Symfony form Builder. It's working correctly. But suppose the user belongs to a Company which is based in Spain. For that User I want to set the default country to be Spain and then show the rest of the countries. How can I do this in Symfony3.
I tried this but its not working.
$builder->add("country", CountryType::class, array(
"label" => "Country",
"required" => false,
"preferred_choices" => array(
"ES" => "Spain",
),
));
Thank you for your time.
In this type Country::class, for use the preferred selection on array you should use:
->add('country', CountryType::class, [
'preferred_choices' => ['DE'],
'label' => 'address.form.country.label',
'attr' => [
'class' => 'form-control',
'placeholder' => 'address.form.country.placeholder'
],
'label_attr' => [
'class' => 'col-sm-2 col-form-label'
],
])
You can use the data option to set the default country.
preferred_choices will put countries at the top of the list, but it will not set them as the default selected option.
This should be the way to go:
->add(
'Country',
CountryType::class,
[
'data' => 'ES',
'label' => 'Country',
]
)
(tested with Symfony 4 and 5)
Related
im learning Symfony 4. I'm trying to make 2 choice type in the form; 'folder' and 'file'. I want 'file' choice type to be activated so that I can select it when I select the 'folder' choice type. That is, 'file' choice type must be deactivated or hidden until 'folder' choice type is selected.
However, I have no idea how to achieve those functions.
$builder
->add('test_name', TextType::class)
->add('folder', ChoiceType::class, [
'label' => 'Select Folder',
'required' => false,
'choices' => [
'folder0' => 'folder0',
'folder1' => 'folder1',
'folder2' => 'folder2'
])
->add('file', ChoiceType::class, [
'label' => 'Select File',
'required' => false,
'choices' => [
'File0' => 'File0',
'File1' => 'File1',
'File2' => 'File2'
])
I was wondering if there is a way to make the tristate version of SwitchInput widget as a filter for a bool column in a GridView?
It’s rendering correctly but it’s not updating on change (Select2 does though).
[
'attribute' => 'enabled',
'format' => 'boolean',
'filter' => SwitchInput::widget([
'name' => 'DeviceSearch[enabled]',
'options' => [
'class' => 'form-control'
],
'value' => $searchModel->enabled,
'tristate' => true
])
]
Last column:
If you use GridView from Kartik it lets extend the base colum to add filterType, so:
[
'attribute' => 'enabled',
'format' => 'boolean',
'filterType' => GridView::FILTER_SWITCH,
]
Will generate an SwitchInput widget as a filter on top, you need to configurate the rest of parameters, or at least the required ones for the switch input filter type.
I'm trying to style the dropdown options (label and checkbox) of a symfony form but am running into problems. I can style the rendered group of checkboxes and labels, but not each item (paired label + checkbox) individually.
I've attempted to style them by:
{{ form_widget(form.qualifications, {'attr': {'class':'d-block'} }) }}
But as detailed in the documentation, this only applies the styles to the parent element (the rendered group of options to select), not each individual option.
Here's the symfony form builder part which creates the widget
->add('qualifications', EntityType::class, [
'class' => Tag::class,
'multiple' => true,
'expanded' => true,
'required' => false,
'placeholder' => 'Select...',
'choices' => $this->tagsService->getTagsQualificationLevels(),
'attr' => [
'class' => 'form-control-ajax-submit-on-change w-20',
]
])
I expect to be able to apply styles to the choices array, but am really stuck on how to do so.
You can use the choice_attr option:
->add('qualifications', EntityType::class, [
'class' => Tag::class,
'multiple' => true,
'expanded' => true,
'required' => false,
'placeholder' => 'Select...',
'choices' => $this->tagsService->getTagsQualificationLevels(),
'attr' => [
'class' => 'form-control-ajax-submit-on-change w-20',
],
'choice_attr' => function($choiceValue, $key, $value) {
return ['class' => 'my_custom_choice_class'];
},
])
May be you should try for loop in template?
I had similar problems with radiobutton, take a look: How to make RadioButton in Symfony Form?
It is even possible to make ?
->add('product', CollectionType::class, [
'entry_type' => EntityType::class, array(
'data' => $options['product'],
'placeholder' => 'Wybierz klienta',
'multiple' => true,
'class' => Products::class,
'attr' => ['class' => 'chosen-select','data-placeholder'=>'Wybierz produkt'],
'choice_label' => function ($product) {
return ''.$product->getJson()["products"]["name"] .' | Stan Magazynowy: '.$product->getJson()["products"]["stock"].'';
},
'label' => 'Wybierz produkty'
),
'entry_options' => [
'label' => 'Value',
],
'label' => 'Add, move, remove values and press Submit.',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'required' => false,
'attr' => [
'class' => 'my-selector',
],
])
I'll try to add chosen list of Products::class in CollectiontType, if some one wanna add product to new order, can add new EntityType and select product, and after submit i`ll handle this as array and save it to order.
If someone has another idea how to add products to form with quantity and then post it to array, please free to write :)
I think this is not the best way to do that.
You just have to add CollectionType to the FormType and update you ProductType class to handle if this is selected or not. May be you have to create a custom formtype for products for order only :-/ . All hard work are on the javascript side
I`ve got a dropdown list in my Symfony2 form like this:
$builder->add('categories','entity', array(
'class' => 'MyBundle:Myentity',
'property' => 'name',
'label' => 'Mylabel',
'expanded' => false,
'multiple' => false,
'label_attr' => array ( 'class' => 'control-label' ),
'attr' => array ( 'class' => 'form-control',
'placeholder' => 'Placeholder',
'title' => "Mytitle",
'data-toggle' => 'tooltip',
'data-myidfromDB' => '????',
'data-mynamefromDB'=>'????' etc. )));
So I am getting a list of MyBundle:Myentity objects and when I choose one I want to show all its properties (like ID, name, etc.) which are stored in my DB and described in Entity class, in different html data-* fields. If I select another one from the list I want to see all information related to my newly selected option in HTML (to change dynamically). Any ideas how to do that?
Since Symfony 2.7 you can set the option choice_attr to ChoiceType and set it a callable receiving the choice as an argument.
EntityType inherits this option and the choice in that case is the instantiated entity, so you can write something like :
$builder->add('categories','entity', array(
'class' => 'MyBundle:MyEntity',
'property' => 'name',
'label' => 'Mylabel',
'attr' => array('class' => 'form-control'),
'label_attr' => array('class' => 'control-label'),
'choice_attr' => function (\AppBundle\Entity\MyEntity $myEntity) {
return array(
'data-private-property' => $entity->getPrivateProperty(),
'data-some-value' => $entity->someMethod(),
);
},
);
You can't do that in easy way.
But you can put more information in select label.
Look on
http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label
Yout can put here more field details and get it from your javascript.