How to customize one specific field in Symfony Forms - forms

I am rendering a form that contains several choice fields. I want to render one specific choice field differently from the other ones.
I've tried to use:
{% form_theme form 'MyBundle:Form:my-choice-field.html.twig' %}
{{ form_widget(field) }}
But that changes the look and feel of all choice fields in that form. If I place form_theme right next to the field I want to render differently, it does nothing.
I have thought of creating a new Form Type but it seems overkilling, I just want a different style for that specific field. Any ideas?

In the doc there is a section describing how to personalize an individual field, for example to customize the name field of a product entity only:
{% form_theme form _self %}
{% block _product_name_widget %}
<div class="text_widget">
{{ block('form_widget_simple') }}
</div>
{% endblock %}
{{ form_widget(form.name) }}
Here, the _product_name_widget fragment defines the template to use for the field whose id is product_name (and name is product[name]).
You can also override the markup for an entire field row using the same method:
{% form_theme form _self %}
{% block _product_name_row %}
<div class="name_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endblock %}
{{ form_row(form.name) }}

Related

How to customize both DIV and TABLE form widgets without duplicating code in symfony

Trying to customize Symfony form rendering, I have the following in my config.yml
twig:
form_themes:
- 'form/fields.html.twig'
The form/fields.html.twig contains a customized number widget:
{%- block number_widget -%}
<input type="text" {{ block('widget_attributes') }} value="{{ value|filter }}" />
{%- endblock number_widget -%}
Everything works fine as long as I use default form layout (div). However, if I want the form to have table layout, I try to add something like this in the template:
{% form_theme 'form_table_layout.html.twig' %}
The form gets displayed as table, but then my customized number_widget is no longer customized!
I have also tried:
{% form_theme 'my_cystom_theme.html.twig' %}
with my_cystom_theme.html.twig containing:
{% use 'form_table_layout.html.twig' %}
Did not help.
Question:
Is there a way to make application wide customization of a widget for both div and table layout forms without duplicating the code?
The solution is to use a custom tempalte with multiple 'use' blocks:
create custom table-layout form, for example form/table_layout.html.twig, such as:
{# original table layout template #}
{% use 'form_table_layout.html.twig' %}
{# our custom field widgets #}
{% use 'form/fields.html.twig' %}
{# define more custom widgets for table layout here #}
In order to render a table layout form with our custom widgets, use this in the template:
{# form_theme 'form/table_layout.html.twig' #}
Do the same for div layout. Custom widgets from form/fields.html.twig will be used in both table and div layouts without duplicating the code.

How do I display a specific post in Jekyll?

I'm wanting to use Jekyll as a CMS essentially, so I would like to take the content from a post and display it in a specific area of my website.
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
This displays all of the content from all of the posts, however I'd like to take content from one post at a time. I'm fairly new to Jekyll, so I'm not sure if i'm supposed to add YAML front matter into my posts and target them with a "for post in site with title_____" post.content" of sorts.
Thank you!!
{% assign thepost = site.posts | where:"slug","post_slug" %}
{% for post in thepost %}
{{ post.content }}
{% endfor %}
And on the post;
slug: post_slug
I figured it out, sorry I posted prematurely.
The answer is add a category to your post with YAML front matter and then in your include file use:
{% for post in site.categories.CATEGORYNAMEHERE %}
{{ post.content }}
{% endfor %}

Customize form_row for different form types

I want to set different form_row layout for different form type. I found in templates block named "choice_widget_collapsed", but it render only select tag with options.
I cannot find where this block is being used. Actually it seems that it is rendered instead of form_widget block. I suppose there is somewhere switch/if structure which checks form type and renders appropriate block, but i dont know where to find this switch, or dont know how to check input type inside form_row block.
I know that block type can be found inside form.vars.block_prefixes array, but this sux, because its position may change in the future as it was already.
So the question is: how can i make form_row display different thing depending on form field type?
You should override the normal block in a theme of yours. This block should work like this:
{% block form_appropriate_block %}
{% spaceless %}
{% if form.vars.widget = 'myIntendedWidgetType' %}
[yourTemplate]
{% else %}
{{ parent() }}
{% endif %}
{% endspaceless %}
{% endblock form_appropriate_block %}
And then, in your template, activate your theme using:
{% form_theme form 'MyBundle:Form:formTheme.html.twig' %}
In this way, your form theme is used only when needed, and if the type is not the one you want, it falls back to the normal behaviour.

Errors for form in rendered template

I have this :
template edit.html.twig
{% extends "....:...:layout.html.twig" %}
{% block body %}
{{ render(controller('EVeilleurFlowerBundle:Catalogue:add' )) }}
{{ render(controller('EVeilleurFlowerBundle:Catalogue:delete' )) }}
{% endblock %}
And the template "add" is basically a single form.
When the user enters "wrong" data in the add form, it automatically redirects to add.html.twig, displaying a single form ( wtih the error ) without layout, css etc...
Can i change the redirection, or am I building my templates the wrong way ? (pretty much newbie here :) )
Thanks

How do you setup one or more different form_row templates in Symfony 2?

I have a project in Symfony2 where I have lots of different forms that do lots of different things and one thing I am struggling with at the moment is working out how I can set two different templates to handle form_rows.
I have specified one form_row template in a fiels.html.twig file that handles form rows as follows:
{% block form_row %}
{% spaceless %}
<div class="form-element{% if errors %} form-element-error{% endif %} widget-group clearfix">
<div class="widget widget-1-of-3">
{{ form_label(form) }}
</div>
<div class="widget widget-2-of-3">
{{ form_widget(form) }}
{% if errors %}
<small>{{ form_errors(form) }}</small>
{% else %}
{% if help is defined %}
<small>{{ help }}</small>
{% endif %}
{% endif %}
</div>
</div>
{% endspaceless %}
{% endblock form_row %}
This works great for me for general forms, but I also have a number of form elements that I have in tables as well. This is generally for groups of form data that I am updating.
In this case I then want to be able to specify using a different form_rows template.
I understand that I could specify the row via the field name or group, but I have a lot and I just want a more general way of doing this.
Does anyone know how this could be achieved.
You can have a lot of form themes defined in different templates.
Just add in your twig file on the top where you want to use theme:
{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}
{{ form_widget(form) }}
where form_theme file can be any twig file with theme definition.
Now form widget is decorated with attached theme.