Arkounay / ux-collection

Collection for Symfony forms using Symfony UX
MIT License
37 stars 5 forks source link

How to use custom prototype #14

Closed N445 closed 2 months ago

N445 commented 2 months ago

How can I create and use custom prototype with your bundle ? I don't see any exemple on your doc

Arkounay commented 2 months ago

If you want to customize the twig template, it should work like Symfony's CollectionType e.g

$form->add('myCollection', UxCollectionType::class, [
    'entry_type' => MyEntryType::class,
    // ...
])
class MyEntryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
            // ...
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => MyEntry::class,
            'block_prefix' => 'my_custom_entry'
        ]);
    }
}

Then in your form_theme

{% block my_custom_entry_widget %}
    <div>
        {{ form_row(form.field1) }}
        {{ form_row(form.field2) }}
    </div>
{% endblock %}

Same if you want to change the prototype_name , or add directly some css class through the form type

$form->add('myCollection', UxCollectionType::class, [
    'entry_type' => MyEntryType::class,
    'entry_options', ['row_attr' => ['class' => 'col-xl-2 col-lg-4']],
    'attr' => ['class' => 'collection arkounay-ux-collection arkounay-ux-horizontal-collection row gy-3']
    'prototype_name' => "__custom_prototype__",
])

If you want to change only the prototype itself and not the rest, since symfony 6.1 there is also the prototype_options attribute if you want to change only the prototype itself, but I've never used it

N445 commented 2 months ago

it's work thank for this fast answer !