Html translation with parameters in symfony3 / twig - forms

I have a pb with a translation containing parameters (link href). I have added a |raw but I see the html displayed.
My code :
{{ form_row(form.valid, {
'label' : 'annonces.form.valide_cgu_cgv' | trans ({
'cgu_link' : path('page_statique', {'page' : 'cgu'}),
'cgv_link' : path('page_statique', {'page' : 'cgv'})
}) | raw }) }}
What I see :
Je reconnais accepter les Conditions générales d'utilisation et Conditions générales de vente
Any idea ?
Thank you !

As the form_label use trans() without the raw inside of its code
{%- block form_label -%}
{# [...] #}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
{%- endif -%}
{%- endblock form_label -%}
You need either to use a custom form_label or 'manually' display the label

Related

Symfony Bootstrap 4 Form Layout Inheritance

I'm trying to override some fields of bootstrap_4_layout.html.twig, but i found some problems with radio_widget block. I need to change the div element to label, but when I ovverride the block, the radio element is duplicated on view. The div in question is that with class custom-control
{% block radio_widget %}
{% set parent_label_class = parent_label_class|default(label_attr.class|default('')) %}
{% if 'radio-custom' in parent_label_class %}
{% set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) %}
<div class="custom-control custom-radio{{ 'radio-inline' in parent_label_class ? ' custom-control-inline' }}">
{{ form_label(form, null, { widget: parent() }) }}
</div>
{% else %}
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) %}
<div class="form-check{{ 'radio-inline' in parent_label_class ? ' form-check-inline' }}">
{{ form_label(form, null, { widget: parent() }) }}
</div>
{% endif %}
{% endblock radio_widget %}
Analizyng the dom with the console, I see that are created two element with custom-control class. Why?
I found the solution. I override the block radio_widget and changed the div element with label:
{% block radio_widget %}
{% set parent_label_class = parent_label_class|default(label_attr.class|default('')) %}
{% if 'radio-custom' in parent_label_class %}
{% set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) %}
<label class="custom-control custom-radio{{ 'radio-inline' in parent_label_class ? ' custom-control-inline' }}">
{{ form_label(form, null, { widget: parent() }) }}
</label>
{% else %}
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) %}
<div class="form-check{{ 'radio-inline' in parent_label_class ? ' form-check-inline' }}">
{{ form_label(form, null, { widget: parent() }) }}
</div>
{% endif %}
{% endblock radio_widget %}
Then I override also the block checkbox_radio_label. Inside it I substituted the base code:
// ...
{{ widget|raw }}
// ...
With:
{% set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) %}
<input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
For reference about bootstrap 4 twig blocks see this link:
https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

How can I put the asterisk of my required field on my label? (Symfony form)

I am working on Symfony 3 and I have some trouble with my form.
When I create a Symfony form with a field not required, here is my code :
I create the form :
$form = $this->createFormBuilder()
->add('prenom' TextType::class, array(
'label' => 'Votre prénom',
'required' => false
)
->getForm();
Here is the code in my view for this field:
{{ form_label(form.prenom) }}
{{ form_errors(form.prenom) }}
{{ form_widget(form.prenom) }}
And this is the HTML I have :
<label class="control-label" for="contact_prenom">Votre prénom</label>
<input type="text" id="contact_prenom" name="contact[prenom]" class="form-control"/>
Now if I do the same without the 'require' => false on my FormBuilder, here is the HTML I get:
<label class="control-label required" for="contact_prenom">Votre prénom</label>
<sup class="required" title="Champ obligatoire">
<i class="fa fa-asterisk"></i>
</sup>
<input type="text" id="contact_prenom" name="contact[prenom]" required="required" class="form-control" />
Is it possible to control the "sup" tag so the asterisk * can be with my label?
I guess I can do it with jQuery, but I'd like to know if it is possible to do it on my form builder or in Twig?
In the doc there is a specific section here http://symfony.com/doc/current/form/form_customization.html#adding-a-required-asterisk-to-field-labels
You can even do with CSS only
label.required:before {
content: "* ";
}
As of Symfony 5.1 you can do the following
->add('name', TextType::class, [
'label' => 'Name <span class="badge badge-danger badge-pill">Required</span>',
'label_html' => true
])
The label_html (bool) property will allow HTML to be injected into the label directly and render on the form output.
Documentation - https://symfony.com/doc/current/reference/forms/types/form.html#label-html
Yes, you could override the twig template or the block that symfony uses to render your widget, have a look at:
http://symfony.com/doc/current/templating/overriding.html
In your case, you're looking for
vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
And this would be the block you want to override:
{%- block form_label -%}
{% if label is not same as(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif -%}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
{%- endif -%}
{%- endblock form_label -%}

Html in label choice form

In choiceType field I have parameter - choice label
'choice_label' => function ($pay, $key, $index) {
return "<b>".$pay->getName()."</b><br />";
},
And render function of fields in twig:
{{form_widget(field)}}
{{form_errors(field)}}
Of course I want to render getName in bold characters,
I have try with twig autoescape and {{form_widget(field)|raw}} - without success
You have to custom the choice_label form in a custom theme (ex : radioHTML.html.twig) => I just add |raw to the label text
{%- block form_label -%}
{% if label is not same as(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif -%}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
{%- if translation_domain is same as(false) -%}
{{- label|raw -}}
{%- else -%}
{{- label|trans({}, translation_domain)|raw -}}
{%- endif -%}
</{{ element|default('label') }}>
{%- endif -%}
{%- endblock form_label -%}
Your form type just return html string in the choice_label option :
$builder->add('fieldName', EntityType::class, array(
'required' => true,
'class' => 'AppBundle\EntityName',
'choice_label' => function (EntityName $entity) {
$html = '<div>';
$html .= $entity->getName();
$html .= '</div>';
return $html;
},
'data' => null,
'multiple' => false,
'expanded' => true)
And then use it for your specific field like that in your twig TPL :
{% form_theme from.fieldNalme 'form/radioHTML.html.twig' %}

Symfony2 twig forms

Symfony2 uses some twig templates when rendering forms.
In particular, in order to render the choice form field collapsed, symfony2 uses the following snippet of code (from form_div_layout.html.twig):
{%- block choice_widget_collapsed -%}
{%- if required and placeholder is none and not placeholder_in_choices and not multiple -%}
{% set required = false %}
{%- endif -%}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{%- if placeholder is not none -%}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? placeholder|trans({}, translation_domain) }}</option>
{%- endif -%}
{%- if preferred_choices|length > 0 -%}
{% set options = preferred_choices %}
{{- block('choice_widget_options') -}}
{%- if choices|length > 0 and separator is not none -%}
<option disabled="disabled">{{ separator }}</option>
{%- endif -%}
{%- endif -%}
{%- set options = choices -%}
{{- block('choice_widget_options') -}}
</select>
{%- endblock choice_widget_collapsed -%}
In my application, I want to create a new form type, that extends the default collapsed choice type, but where choice list has custom formatting.
In other words, for my form field type, named places_widget, extending choice_widget_collapsed block but redefining choice_widget_options.
I already tried:
/* My first attempt */
{%- block places_widget -%}
{% block choice_widget_options %}
{{ block('choice_places_widget_options') }}
{% endblock choice_widget_options %}
{{ block('choice_widget_collapsed') }}
{%- endblock places_widget -%}
/* My other attempt */
{%- block places_widget -%}
{{ block('choice_widget_collapsed', { 'choice_widget_options' => choice_places_widget_options }) }}
{%- endblock places_widget -%}
/* together with */
{%- block choice_places_widget_options %}
// code here
{%- endblock choice_places_widget_options -%}
None of the two solutions is working, and actually I have the feeling I am doing things in the wrong way.
Does anyone have any idea on how to achieve the goal?
Thank you in advance
Forms can be customized using themes (see the doc here: http://symfony.com/doc/current/cookbook/form/form_customization.html#form-theming-in-twig)
In your case, it seems you need to load your own theme with:
{% form_theme form _self %}
Note that you could externalize these blocks in another file (refer to http://symfony.com/doc/current/cookbook/form/form_customization.html#method-2-inside-a-separate-template to see how to do that)

fosUserBundle sends email registration empty

so when i registre via fosUserBundle form on production env it sends an email to my gmail but there is no confirmation link in the email, there is just this
registration.email.message
in the title and in the body of the email, someone knows why ?
It's because the email is content obtained using translator and you have wrong configuration.
Make sure you have the translator enabled:
# app/config/config.yml
framework:
translator: { fallback: %locale% }
# app/config/parameters.yml
parameters:
locale: en # default locale
Also if you write your app in different language than english, make sure the key registration.email.message is translated into it. If it's not, you can override the translations by writing following file:
# app/Resources/FOSUserBundle/translations/FOSUserBundle.{your_locale}.yml
registration:
email:
subject: Registration email subject
message: |
Here you can place the content of the email.
It can be multiline and you even have access to
variables %username% and %confirmationUrl%.
This is the FOSUser default mail:
{% block subject %}
{% autoescape false %}
{{ 'registration.email.subject'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl}, 'FOSUserBundle') }}
{% endautoescape %}
{% endblock %}
{% block body_text %}
{% autoescape false %}
{{ 'registration.email.message'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl}, 'FOSUserBundle') }}
{% endautoescape %}
{% endblock %}
{% block body_html %}{% endblock %}
At line 8, 'registration.email.message' is the email content. And trans is a replace filter. Try something like this:
{% block subject %}
{% autoescape false %}
{{ 'Confirmez votre inscription sur blabla.com'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl}, 'FOSUserBundle') }}
{% endautoescape %}
{% endblock %}
{% block body_text %}
{% autoescape false %}
{{ 'Bonjour %username%
Merci de cliquer sur le lien suivant afin de confirmer votre inscription sur blabla.com:
%confirmationUrl%'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl}, 'FOSUserBundle') }}
{% endautoescape %}
{% endblock %}
{% block body_html %}{% endblock %}