Symfony - default (empty) entries in form being rendered? - php

I have 2 simple Symfony questions
Question 1: Using FormBuilderInterface in Symfony 4, I have this segment of code:
->add('actions', CollectionType::class, [
'entry_type' => ActionType::class,
'by_reference' => false,
'collection_item_class' => '',
'prototype_name' => 'action',
'prototype_data' => new Action(),
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false,
'delete_empty' => true,
])
This is rendering an empty row in the form, see below:
empty row being rendered
I can't find anywhere to stop rendering a blank row.
Question 2: When using FormBuilderInterface, how do I introduce simple conditional logic to the below segment:
->add('statusChoice', ChoiceType::class, [
'expanded' => true,
'choices' => [
'Approve' => 'approved',
'Decline' => 'declined',
]
])
->add('declinedReason', TextareaType::class)
'declinedReason' would be required if the person has chosen 'Decline'/'decline' in the above radio button. I'm aware how to do this with something like jQuery, but not sure with Symfony.
Thank you :)

Related

How do I style child elements of a drop down list in a Symfony form

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?

One symfony form block is displayed by default, but I need zero blocks

I built a new form and by default there's one form-block displayed in the collection of blocks.
I want to have zero form-blocks in this collection by default.
What parameter is responsible for the number of form-blocks/entries in the collection by default?
I've attached the buildForm function for the collection in my ClassType.php file.
$builder
// [..]
->add(
'images',
CollectionType::class,
[
'entry_type' => StoAttachmentType::class,
'by_reference' => false,
'allow_add' => !$options['disabled'],
'allow_delete' => !$options['disabled'],
'prototype_name' => '__attachments__',
'disabled' => $options['disabled'],
'attr' => [
'class' => 'collection',
'data-prototype-name' => '__attachments__',
'data-add' => $options['disabled'] ? '0' : '1',
'data-allow-delete' => $options['disabled'] ? '0' : '1',
'data-links-template' => 'collection',
'data-file-preview' => '1',
'data-help' => 'Sto.images.help'
],
'label' => 'Sto.images',
'entry_options' => [
'disabled' => $options['disabled'],
'multiple' => true,
'withMain' => true
],
]
)
StoAttachmentType consist of three fields. The first one is required => True. Others fields is required => False.
Problem was resolved by overwriting class, but it's too much code. Is there any easier way?

How to remove empty Datetime entries of Symfony form Collection ? (delete_emply not working)

I am using a Symfony form with a collection of Datetime entries. How can I remove the empty datetime entries from the collection when the form is submitted?
I have set delete_empty to true but it is not working. I am trying hard to follow the doc instructions
(https://symfony.com/doc/current/reference/forms/types/collection.html#delete-empty) but I must be missing something somewhere.
I have also tried the required option to false and empty_data to null.
Should I write a callable that check if datetime is empty? If so how to check if a datetime is empty?
Here is my form builder code:
$form = $this->createFormBuilder($event)
->add('datetimes', CollectionType::class, [
'entry_type' => DateTimeType::class,
'entry_options' => [
'date_widget' => 'single_text',
'input' => 'string',
'time_widget' => 'single_text',
'html5' => false,
'required' => false,
'empty_data' => null,
],
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'prototype' => false,
'delete_empty' => true
])
->add('submit', SubmitType::class)
->getForm();
Can someone tell me what I am missing or doing wrong here?

CollectionType of EntityType Symfony Forms

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

Collection type: First input is being ignored

I am having troubles with the collection type. I can't make it save the first input.
here is my form:
->add('urls', CollectionType::class, [
'entry_type' => UrlType::class,
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false,
'label' => 'my label',
'label_attr' => ['class' => '...'],
'required' => false
])
I slightly adjusted my collection_widget and url_widget. My main concern is that the fist input in the collection has id="form_urls" name="form[urls]" as its attributes. And the following ones (I made them appear by clicking on the button +) has right attributes id="form_urls_1" name="form[urls][1]", id="form_urls_2" name="form[urls][2]".
All that results in inability to save the first input (it just ignores it). How can I solve the problem?

Categories