Display collection form on page load in Sonata Admin Bundle - php

Is there an option or a known way to display collection form on page load instead of clicking on "add" button in order to display it ?
I trigger a click event on page load but its not the expected behavior..
Any help would be much appreciated.

Alright, I achieved what I wanted by setting a default collection array on my form field, each Entity object in array imbricates a form, since I needed to display 3 form on page load I instanciated 3 entity, quite logic when I think of it now but a dedicated option could be nice though.
->add('details', CollectionType::class, [
'data' => [new OfferDetail(), new OfferDetail(), new OfferDetail()],
'label' => false,
'required' => true,
'type_options' => [
'delete' => false,
],
], [
'edit' => 'inline',
'inline' => 'table'
])

Related

Kartik Select 2 - Programatically changing multiple

I have a yii2 activeform where the functionality of the form can change based on other things within the form. So, I have a clubs field, that can be multiple in some instances but not multiple in others.
<?= $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'data' => $club_data,
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }'),
],
],
])->label('Club(s)'); ?>
I need to programmatically change the multiple pluginOption to true and false. This should be when the user goes into the form but also immediately when a dropdown is changed on the form. I can do it when the user initially goes into the form but not immediately when another dropdown is changed.
I've made separate fields, one actually linked to a field in the database and another not, this kind of works but it's far from elegant!
I've looked in both the kartik select2 documentation and the standard jquery select2 documentation and I can't spot anything. Any help or pointers would be much appreciated.
Reference
I was working on a Yii2-formwizard plugin a few months back where I had a situation of providing tabular functionality and cloning the fields really became the pain in the ass when it came to using 3rd party plugins like Select2 and Datepicker and I figured out the following code along with a lot of other (as the other part isn't relevant to your problem so not adding it).
Approach to the problem
The way Kartik-select2 works it stores the select2 plugin-specific options in the data-krajee-select2 attribute of the element you are using the select2 on.
And the theme or say the Kartik's plugin-specific options like theme and others are saved in the variable whose name is saved in the data-s2-options attribute. You can look for both of them in view-source of the page where you are using it.
Solution
So what you need to do is to
Steps
Get the applied options
Add/Update the option
Apply back the options
As you just added a single field and not the other one on who's selection it would change, I would demonstrate an example where you can change the behavior of the select2 from multiple to single using 2 different buttons, clicking on the relevant button the select2 will work accordingly. You can adjust the javascript code where ever you want to.
But before I add any code you need to fix one thing, you don't need the main data option when you are using the ajax option inside the plugin options. In other words, it does not have any effect on your select2 and is adding page load time only you should remove it.
You select2 should look like below, I added id to the options of the select2 for the example you can change it to the active form formatted name like model-field_name format if you like, but don't forget to change the id in the javascript code too
<?php echo $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)','id'=>'clubs'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
]
]
])->label('Club(s)');?>
Add the following buttons on your page
echo Html::button('multiple', ['class' => 'btn btn-info', 'id' => 'multi']);
echo Html::button('single', ['class' => 'btn btn-info', 'id' => 'single']);
And now the magic thing, add it on the top of your view
$js = <<<JS
var element=$("#clubs");
$('#multi').on('click',function(e){
//reset select2 values if previously selected
element.val(null).trigger('change');
//get plugin options
let dataSelect = eval(element.data('krajee-select2'));
//get kartik-select2 options
let krajeeOptions = element.data('s2-options');
//add your options
dataSelect.multiple=true;
//apply select2 options and load select2 again
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
$('#single').on('click',function(e){
element.val(null).trigger('change');
let dataSelect = eval(element.data('krajee-select2'));
let krajeeOptions = element.data('s2-options');
dataSelect.multiple=false;
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
Now if your select2 is working correctly click on the multiple button and you will see the multiple selections are enabled for the select2 and when clicked on the single button.
Hope it helps.

How to set different entry_options for each CollectionType entry in Symfony FormBuilder

I'm trying to create multiple HiddenType fields in with Form Builder using CollectionType, to group these fields when I'm dealing with them in my controller. But I want each field to have different attributes. Is it possible to use entry_options to set different options for each entry?
I'm using a javascript drag and drop plugin to drop elements in a container, when these elements are dropped a JS code updates these hidden fields that, I use to capture the information in my controller. So is mandatory that these fields are grouped somehow so I can deal with them easily in my controller.
An example:
$builder->add(self::FIELD_MAPPED_COLUMNS, CollectionType::class, [
'entry_type' => HiddenType::class,
'data' => ['field1', 'field2', 'field3'],
'label' => false,
'entry_options' => [
'attr' => [
'data-my-field' => '?????'
]
]
])->add('submit', SubmitType::class);
I want to set a different data-my-field for each HiddenType so I can use it in Javascript. Is it possible?
I just faced the same problem. Here is my workaround.
First add a template in the entry_options:
$builder->add(self::FIELD_MAPPED_COLUMNS, CollectionType::class, [
'entry_type' => HiddenType::class,
'data' => ['field1', 'field2', 'field3'],
'label' => false,
'entry_options' => [
'attr' => [
'data-my-field' => '%templateForMyFieldByFieldData%'
]
]
])->add('submit', SubmitType::class);
Then render each field of the collection individually in a Twig loop and replace the template by the required value:
...
{{ (form_widget(field) | replace('%templateForMyFieldByFieldData%', valueToInsert)) | raw }}
...
Hopefully it will be usefull to someone !

Symfony Sonata project: How to add multiple input texts to block?

I would like to add a collection of input text with same name (i.e. name="blabla[]") filed to admin block with add/delete buttons.
I'm using collection form field type but can't see add/delete buttons
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('title', 'collection',
array('type' => 'text' ,
'required' => true,
'allow_add' => true,
'data' => array('First' => 'One')
)
)
)
));
}
I get below result without add/delete buttons!
Any idea how to get it working ?
I think you should use sonata_type_collection or sonata_type_native_collection instead of collection.
Here is an extract of the field doc :
14.1.7. SONATA_TYPE_NATIVE_COLLECTION (PREVIOUSLY COLLECTION)
This bundle handle the native Symfony collection form type by adding:
an add button if you set the allow_add option to true. a delete button
if you set the allow_delete option to true.

Displaying index instead of "__name__label__ *"

Using sonata admin Bundle And I have A form to Define the A "Question" model, I have an attribute "choices" which is a collection :
->add('choices', 'collection',
array('allow_add' => true,'allow_delete' => true, 'block_name' => false,
'delete_empty' => true, 'label' => 'choices'))
When I add a choice The label wrote in his top is "_name__label__*", I need to change it to the choice index, anyone found a solution for this ? thank you

Select existing entity or create a new one

I've read Symfony 2 Entity field type with select and/or add new and this Symfony2 Form : Select an entity or add a new one but those topics didn't solve what I want.
I want for user to choose from existing entities but if he wants to, should be able to create a new one after clicking on a button ( which will render form ). I know how to render entities in select, I know how to render field which allows user to create few new entities ( collection ) but I don't know how to render both of them together.
right now I have select:
->add('place', 'entity', array(
'required' => false,
'class' => 'MyBundle\Entity\Place',
))
but as I said - I want to allow user to add new entity if he wants to. I was trying with collection:
->add('place', 'collection', array(
'required' => false,
'data' => [new Place()],
'type' => new \MyBundle\Form\Place\PlaceType(),
'allow_add' => true,
'options' => array( 'label' => false),
))
but that only allowed user to create new entities, not to select from existing ones...
If you want them to be able to add a new entity and select an existing one i'd suggest you take a look at form events.
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

Categories