Adding "help" messages to fields - symfony

I'm trying to add some help messages after each field in form in symfony2.
I have read about one solution in official docs : http://symfony.com/doc/current/cookbook/form/form_customization.html#adding-help-messages
But this solution makes little sense, because we've need to create all form manually.
For example, it easy to define label: $formBuilder->add('myfieldname', 'text', array('label'=>'some my field label')); But how to pass help messages? (In other words, some custom variables)

A another method without another extension :
In your form builder class:
$builder->add('yourField',null, array('attr'=>array('help'=>'text help')))
In your form template rewrite:
{% block form_row %}
{% spaceless %}
{{ form_label(form) }}
{{ form_widget(form) }}
{% for attrname, attrvalue in attr %}
{% if attrname == 'help' %}
<span class="help-block">{{ attrvalue }}</span>
{% endif %}
{% endfor %}
{{ form_errors(form) }}
{% endspaceless %}
{% endblock form_row %}

$formBuilder->add('myFieldName', 'text', array('help' => 'My Help Message')); But it think you also need to add an extension that add this as a default option for all forms :
https://github.com/simplethings/SimpleThingsFormExtraBundle#helpextension
This makes you able to edit attributes directly from you FormTypes.

Since symfony 4.1 you can do :
$builder->add('email', null, [
'help' => 'Make sure to add a valid email',
]);
https://symfony.com/blog/new-in-symfony-4-1-form-field-help

You can use the solution in the official docs as you described.
But, the work is not complete yet. You have to create a Form Type Extention, based on this article: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html
After complete the Form Type Extention creation you can add Help Messages like this:
$form = $this->createFormBuilder()
->add('name', 'text', array(
'help' => 'this is a help message to user',
))
I think this is a native better solution.
Also, i recommend read this great article that shows you how to enable and set the help option in symfony2 forms:
http://toni.uebernickel.info/2012/11/03/how-to-extend-form-fields-in-symfony2.1.html

A little off topic but still useful if you're planning to use Bootstrap for your project then you can take advantage of some form helpers provided by the Mopa Bootstrap Bundle.
Demo: http://bootstrap.mohrenweiserpartner.de/mopa/bootstrap/forms/help_texts
GitHub: https://github.com/phiamo/MopaBootstrapBundle
Example:
<?php
$form = $this->get('form.factory')
->createNamedBuilder('form_name')
->setMethod('POST')
->add('testSelect', 'choice', [
'choices' => ['val1' => 'Value 1', 'val2' => 'Value 2'],
'required' => true,
'help_block' => 'Here some help text!!!'
])
->add('Save', 'submit')
->getForm();
return $form->createView();

Related

Symfony form builder: How to iterate/render dynamic amount of text fields in twig? (Sylius - exta checkout step)

I added an extra checkout step to the Sylius checkout procedure and I am trying to add 1 text field per instance of an ordered item. Thus having put 3x itemA and 1x itemB in cart should spawn 4 text fields.
I have this code so far in the form builder (based on code from here: https://sf.khepin.com/2011/08/basic-usage-of-the-symfony2-collectiontype-form-field/)
$builder->add('myCollection', CollectionType::class, $formOptions);
$totalCounter = 0;
/** #var OrderItem $item */
foreach($order->getItems() as $item) {
for($itemCounter = 0 ; $itemCounter < $item->getQuantity(); $itemCounter++ ) {
$totalCounter++;
$builder->get('myCollection')->add('item_' . $totalCounter, TextType::class, $formOptions);
}
}
First Question: Is this the right approach for the form builder for my particular scenario, and secondly: If yes, how do I read from this in the twig template?
form.children.myCollection does exist but does not seem to contain these children, so that I could use them with the form_row function. What I would have expected is something like this:
{% set totalCounter = 0 %}
{% for item in order.items %}
{% for itemCounter in 1..item.quantity %}
{% set totalCounter = totalCounter + 1 %}
{{ form_row(form.children.myCollection['item_' ~ totalCounter ]) }}
{% endfor %}
{% endfor %}
Any idea how to do this? Thanks for any hint in advance!
Just saw my exact case is described in the beginning of the documentation of CollectionType https://symfony.com/doc/current/reference/forms/types/collection.html
The form builder part..
$builder->add('emails', CollectionType::class, [
// each entry in the array will be an "email" field
'entry_type' => EmailType::class,
// these options are passed to each "email" type
'entry_options' => [
'attr' => ['class' => 'email-box'],
],
]);
..and the twig part:
{{ form_label(form.emails) }}
{{ form_errors(form.emails) }}
<ul>
{% for emailField in form.emails %}
<li>
{{ form_errors(emailField) }}
{{ form_widget(emailField) }}
</li>
{% endfor %}

Nested forms in symfony empty after form submit

I have a question related to nested forms in symfony.
I have this form
{{ form_start(form, {'attr': {'class': 'needs-validation'}} ) }}
....
<div id="slot-fields-list" data-prototype="
{% filter escape %}
{% include 'slot/form/prototype.html.twig' with {'form': form.slot.vars.prototype} %}
{% endfilter %}"
data-widget-tags="{{ '<span></span>'|e }}">
</div>
{{ form_end(form) }}
.......................................
->add('slot', CollectionType::class, [
'entry_type' => SlotType::class,
'entry_options' => ['label' => false],
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false
])
Work fine, I have data in data-prototype after render template. When I submit the form and if the form has erreurs this nested form is added after submit button and data-prototype is empty. Any ideas ? Thx in advance
The documentation for CollectionType is actually quite good, but perhaps some particular details need reiteration.
First the documentation tells you, how to render the collection items (your slots probably):
<ul class="tags">
{# iterate over each existing tag and render its only field: name #}
{% for tag in form.tags %}
<li>{{ form_row(tag.name) }}</li>
{% endfor %}
</ul>
This is what's missing in your case. Just to avoid confusion, their <ul class="tags"> is your <div id="slot-fields-list" ...>. You have to go through the form.slot with
{% for slot in form.slot %}
{{ form_row(slot) }} <!--- or render your sub-form properly -->
{% endfor %}
now. Symfony is very reluctant to omit form elements. So when you place a {{ form_end(form) }} it will render the rest of the form elements, which have not been rendered before, to your form, before closing the <form>. (the same behaviour can be achieved by {{ form_rest(form) }}, if you ever need it)
That is the reason, why your subforms will appear after your submit button and completely out of place - because they weren't rendered anywhere before.
However, this does not yet explain the missing data-prototype, but perhaps there is a simple explanation as well ...
you possibly already know about the form themes, where you could add your own slot_widget, slot_row, slot_label and have symfony do all that weird include-stuff for you.

Label is not displaying in Symfony form

I am facing a issue to display label.
Following is the code to generate form element.
$builder->add(
'hearAboutUs', 'choice', [
'choices' => ['Online Search'=>'Online Search',
'Email'=> 'Email',
'My Company' => 'My Company',
'Colleague or Friend'=>'Colleague or Friend',
'Existing Client' =>'Existing Client',
'Direct Mail' => 'Direct Mail',
'Other'=> 'Other',],
'label' => 'How did you hear about us?',
'required' => true,
'expanded'=> true,
'multiple' => false,
]
);
I am getting following output.
As like "Notes" label "How did you hear about us" label is not displaying.
You can try to override your form theme about choice widget.
You can do it like this :
1- Create a file named fields.html.twig in app/Resources/views/form/
2- In this file you have to extends default twig layout and override checkbox_widget adding <label> tag :
{% extends 'form_div_layout.html.twig' %}
{% block checkbox_widget %}
{% spaceless %}
<label>
<input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
</label>
{% endspaceless %}
{% endblock checkbox_widget %}
3- Finally, tell Symfony to use it in your view like this :
{% form_theme form 'form/fields.html.twig' %}
My View is looks like
My Updated Entity is
I have handled this using JQuery.
On Form Load I am appending Label and for required I have handled this on the controller.
But this is not proper solution, but by this way my problem solved.

Render choice options separately

I have researched quite a bit and also read the documentation but this thing remains unanswered.
I want to render choice-options separately in twig and a {%for%} loop doesn't seem to work.
Here's the situation:
I have 3 radio buttons which are supplemented with 3 different blocks of html each (therefore I cannot do a loop over the segments as they are different).
Form-Extract (form 'payment'):
array(
'choices' => array(
'paypal' => 'payment.method.paypal',
'bank' => 'payment.method.bank',
'check' => /** #Ignore */
),
'label' => 'payment.method',
'expanded' => 'true',
How could I access the different choice options in twig? I have tried
{{ form_widget(form.payment.method[bank]) }}
and also
{{ form_widget(form.payment.method.bank) }}
Thanks so much for your help.
Stefffen
You might want do it this way:
{% for method in form.payment %}
{% if method.vars.value == "paypal" %}
#Render it in a paypal awesome way
{{ form_widget(method) }}
{{ form_label(method) }}
{% else if method.vars.value == "bank" %}
#Render it in a bank awesome way
{{ form_widget(method) }}
{{ form_label(method) }}
{% endif %}
{% endfor %}
Hope it helps

Link in form label using route

In my registration form i have a checkbox "I accept the terms", and want to link the word "terms" to my terms page.
Is there a way to add a link to a form label, using a route? (preferably without injecting the container in the form)
As the solution above somehow didn't work for me I solved it using the solution suggested here: https://gist.github.com/marijn/4137467
OK, so here i how I did it:
{% set terms_link %}<a title="{% trans %}Read the General Terms and Conditions{% endtrans %}" href="{{ path('get_general_terms_and_conditions') }}">{% trans %}General Terms and Conditions{% endtrans %}</a>{% endset %}
{% set general_terms_and_conditions %}{{ 'I have read and accept the %general_terms_and_conditions%.'|trans({ '%general_terms_and_conditions%': terms_link })|raw }}{% endset %}
<div>
{{ form_errors(form.acceptGeneralTermsAndConditions) }}
{{ form_widget(form.acceptGeneralTermsAndConditions) }}
<label for="{{ form.acceptGeneralTermsAndConditions.vars.id }}">{{ general_terms_and_conditions|raw }}</label>
</div>
The best way is to overwrite the twig block used to render that specific label.
First, check the form fragment naming section of the docs. Then create a new block in your form template with the the appropriate name. Don't forget to tell twig to use it:
{% form_theme form _self %}
For the next step check the default form_label block.
You'll probably only need a portion of it, something like this (I'm leaving the default block name here):
{% block form_label %}
{% spaceless %}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{{ label|trans({}, translation_domain) }}
</label>
{% endspaceless %}
{% endblock %}
As an option, you can do so:
->add('approve', CheckboxType::class, [
'label' => 'Text part without link',
'help' => 'And download it',
'help_html' => true,
])
In Symfony 5.1 there are new form improvements.
HTML contents are allowed in form labels!
HTML contents are escaped by default in form labels for security reasons. The new label_html boolean option allows a form field to include HTML contents in their labels, which is useful to display icons inside buttons, links and some formatting in checkbox/radiobutton labels, etc.
// src/Form/Type/TaskType.php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('save', SubmitType::class, [
'label' => ' Save',
'label_html' => true,
])
;
}
}
In your case you can set form label directly from template and pass the route there.
{{ form_widget(form.acceptTermsAndConditions, {
label: '' ~ "I accept ..."|trans ~ '',
label_html: true
})
}}
My solution was another:
form:
$builder
->add(
'agree_to_rules',
'checkbox',
[
'required' => true,
'label' => 'i_agree_to'
]
);
And html:
<span style="display:inline-block">
{{ form_widget(form.agree_to_rules) }}
</span>
<span style="display:inline-block">
rules
</span>
And looks the same :)
A very simple way to do it would be
{{ form_widget(form.terms, { 'label': 'I accept the terms and conditions' }) }}
You can also do this if you want to use translation
In your translation file for example messages.en.yml add
terms:
url: 'I accept the terms and conditions'
And in your view add
{{ form_widget(form.terms, { 'label': 'terms.url'|trans({'%url%': path('route_to_terms')}) }) }}

Resources