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

Asked
Active3 hr before
Viewed126 times

5 Answers

90%

2 There's no easy way to do this to all types. The most direct would be a form extension to the base form type to allow for a new option. This option would then be available in the templates. Though you'd need to also customize the form widgets to actually use that new option. – Yoshi Sep 22 '16 at 11:28 , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,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'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".

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 %
}
load more v
88%

You'll use functions often to render your fields. Variables, on the other hand, are less commonly-used, but infinitely powerful since you can access a fields label, id attribute, errors and anything else about the field.,Functions for rendering each part of a form;, Form Rendering Functions ,The method and action variables were introduced in Symfony 2.3.

1
2
{
   # render the form and change the submission method #
} {
   {
      form(form, {
         'method': 'GET'
      })
   }
}
load more v
72%

Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (|). Multiple filters can be chained. The output of one filter is applied to the next.,The following variables are always available in templates:, Global Variables , Setting Variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
        {% endfor %}
        </ul>

        <h1>My Webpage</h1>
        {{ a_variable }}
    </body>
</html>
load more v
65%

{# templates/base.html.twig #}

{# ... #}
<div id="sidebar">
    {# if the controller is associated with a route, use the path() or url() functions #}
    {{ render(path('latest_articles', {max: 3})) }}
    {{ render(url('latest_articles', {max: 3})) }}

    {# if you don't want to expose the controller with a public URL,
       use the controller() function to define the controller to execute #}
    {{ render(controller(
        'App\\Controller\\BlogController::recentArticles', {max: 3}
    )) }}
</div>
75%

Here is an example of including a partial Twig template using the with keyword to pass it a variable:,You can do this by passing variables to the included partial template using the with keyword.,Using partial Twig templates is a great way to organise your frontend code because you can reuse code fragments in multiple templates. But what happens if you want to use the same partial template in multiple places and customise its content slightly?,To include this partial template in the node template, I can use the following:

 {
    % include 'paywall.html.twig'
    with {
       'title': 'Sign in with your membership details to attend this event'
    } %
 }
load more v

Other "undefined-undefined" queries related to "Symfony/Twig: how to pass additional data to a form?"