Mr. B.
Mr. B.

Reputation: 8717

Symfony/Twig: how to pass additional data to a form?

I'm looking for a way to manage all the form data within MyFormType.php. So far I've the label and placeholder, but I'd like to store an additional string, e.g. for "more details".

AppBundle\Form\Type\MyFormType.php

// ...
$builder->add('fieldname', TextType::class, [
    'label' => 'My label',
    'attr'  => ['placeholder' => 'My placeholder'],       
    'additionalData' => ['info' => 'More information] // <- ???      
]);
// ...

myForm.html.twig

Label: {{form_label(form.fieldname)}} 
Textfield: {{form_widget(form.fieldname)}}  
Info: {{form_additionalData(form.fieldname, 'info')}}  {# ??? #}

I was thinking about passing the data via an data-*-attribute like:

// ...
'attr' => ['data-info' => 'more information']
// ...

But how to read the data in a good way, without buggy workarounds?

Any best/good practise for this issue?

Thanks in advance!

Upvotes: 2

Views: 5923

Answers (3)

Alvin Bunk
Alvin Bunk

Reputation: 7764

B.

I see that none of the answers show a Twig solution, so I wanted to show a Twig answer in case you want to use that. The format for a form_label verses a form_widget are different, so keep that in mind when you code.

I don't know of any form_additionalData rending specification. See the reference here. But instead there is just "form_label, form_errors, and form_widget" that can be used.

In my example below, I'll show "form_label" and "form_widget" for your reference (with a little variety). This works, I tried it out:

Label: {{ form_label(form.fieldname,null,{
    'label_attr':{
        'id':'my_label_id',
        'class':'my_label_class',
        'data-info':'my label information'
    }
}) }}
Textfield: {{ form_widget(form.fieldname,{
     'attr':{
        'class':'my_text_field_class',
        'data-info':'my-text-field information'
     }
}) }}

Let me know if you have questions.

Upvotes: 2

yceruto
yceruto

Reputation: 9585

By using a form type extension for that probably is a way if it's a common use case in your application:

# src/AppBundle/Form/Extension/FormTypeExtension.php

class FormTypeExtension extends AbstractTypeExtension
{
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        foreach ($options['extra_data'] as $name => $data) {
            $dataName = sprintf('data-%s', $name);
            $view->vars['attr'][$dataName] = json_encode($data);
        }
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array('extra_data' => array()));
        $resolver->setAllowedTypes('extra_data', 'array');
    }

    public function getExtendedType()
    {
        return FormType::class;
    }
}

Register your form type extension:

# app/config/services.yml

services:
    app.form.extension.form_type:
        class: AppBundle\Form\Extension\FormTypeExtension
        tags:
            - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }

If:

$form->add('name', TextType::class, array(
    'extra_data' => array(
        'foo' => 'bar', 
        'baz' => array('name' => 'john'),
    ),
));

Then:

<input type="text" id="form_name" name="form[name]" data-foo=""bar"" data-baz="{"name": "john"}"/>

But how to read the data in a good way, without buggy workarounds?

In some JavaScript context by using jQuery example:

var data = $('#form_name').data();

// all data values are converted to JSON automatically
alert(data.foo); // display: bar
alert(data.baz.name); // display: john

Upvotes: 3

Dmitry Malyshenko
Dmitry Malyshenko

Reputation: 3051

Good practice would be to create a FormType for your needs and configure necessary options for it.

class YourType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'add_info'  => "",
        ));
    }


    public function buildView(FormView $view, FormInterface $form, array $options)
    {
       $view->vars = array_replace($view->vars, array(
            'add_info' => $options['add_info'],
        ));
    }

    public function getBlockPrefix()
    {
        return 'your_type';
    }


    public function getParent() 
    {
        return TextType::class;
    }
}

Then

$builder->add('fieldname', YourType::class, [
    'label' => 'My label',
    'attr'  => ['placeholder' => 'My placeholder'],       
    'add_info' => 'More information'
]);

And then make a widget

{% block your_type_widget %}
    {% spaceless %}
        {{ add_info }} 
            ... other code
    {% endspaceless %}
{% endblock %}

Upvotes: 4

Related Questions