CollectionType of EntityType Symfony Forms - php

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

Related

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

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 :)

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?

Symfony - Categories count with EntityType

I have a search form that gets Categories from database and shows each category name as a checkbox, it's retrieved via EntityType, i want to show count categories in front of the category name, i managed to count each one from the database and put the result in an array like this : $categCount[name] = count;
How can i show in front of each category name her count using EntityType ?
->add('categories', EntityType::class, ['class' => 'AppBundle:Categorie',
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'required' => false,
])
Now. I`m not sure how your entity looks like but if you have the count in the same entity you can do something like:
->add('categories', EntityType::class, ['class' => 'AppBundle:Categorie',
'choice_label' => 'getNameWithCategoriesCount',
'multiple' => true,
'expanded' => true,
'required' => false,
])
and in your entity
public function getNameWithCategoriesCount(){
return $this->getName().' '.$this->getCount();
}
Alternatively(not tested) you can :
->add(
'categories',
EntityType::class,
[
'class' => 'AppBundle:Categorie',
'choice_label' => function ($category) use ($this-em) {
return $category->getDisplayName() $this->em->getRepo..()->getCount($category);
},
'multiple' => true,
'expanded' => true,
'required' => false,
]
)
NOTE: For this approach you need to declare your form as service and pass the entityManager

Access properties and methods of a class while configuring EntityType in Formbuilder::add()

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.

Categories