Symfony2 + Twig : Can't access form rows outside "body" block? - forms

i'm a beginner into Symfony2 and i hope that the community could help me and another peoples with this problem.
I have 2 forms sent to the views, that i render these 2 into 2 differents blocks (main-col and left-col), in fact main-col contains user profile form and left-col contains form to upload the profile picture .. This way the profile picture won't appear
But when the profile picture is into the main-col (body block), it works ...
There is some examples for better understand :
This example will NOT work :
{% extends 'FOOBAR\QuoteMachineBundle::base.html.twig' %}
{% block menu -%}
{% include 'FOOBAR\QuoteMachineBundle::CompanyProfile/menu.html.twig' %}
{% endblock %}
{% block title -%}Edit company{% endblock %}
{% block leftcol -%}
<h3>Company Logo</h3>
{{ form(picture_form) }}
Current Picture :<br/>
<img class="opttool" src="{{ entity.pictureWebPath }}" alt="" />
{% endblock %}
{% block body -%}
{{ form(form) }}
{% endblock %}
But this one WILL
{% extends 'FOOBAR\QuoteMachineBundle::base.html.twig' %}
{% block menu -%}
{% include 'FOOBAR\QuoteMachineBundle::CompanyProfile/menu.html.twig' %}
{% endblock %}
{% block title -%}Edit company{% endblock %}
{% block leftcol -%}
{% endblock %}
{% block body -%}
{{ form(form) }}
<h3>Company Logo</h3>
{{ form(picture_form) }}
Current Picture :<br/>
<img class="opttool" src="{{ entity.pictureWebPath }}" alt="" />
{% endblock %}
I switched the "current picture form" from leftcol to body ... In fact forms only works in body block.
I should precise that I can use normal variables into leftcol block and that will work.
The form in leftcol will be an "empty" form element with that only children element :
<input type="hidden" name="_method" value="PUT">
How I can render picture_form correctly into leftcol block ??
EDIT : Like you asked, i published sample of my base.html.twig
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div class="container">
{% if block('leftcol') is not empty %}
<div class="left-col">{% block leftcol %}{% endblock %}</div>
{% endif %}
<div class="col-main">
<h1>{{ block('title') }}</h1>
<article>
{% block body %}{% block fos_user_content %}{% endblock fos_user_content %}{% endblock %}
</article>
</div>
{% if block('rightcol') is not empty %}
<div class="right-col">{% block rightcol %}{% endblock %}</div>
{% endif %}
</div>
</body>
</html>
Thanks !

I am not 100% sure it will solve your problem but using the block method in a IF statement leads to a new request (meaning everything gets called twice). You should try this:
{% set _block = block('leftcol') %}
{% if _block is not empty %}
<div class='left-col'> {{ _block|raw }} </div>
{% endif %}
Instead of
{% if block('leftcol') is not empty %}
<div class="left-col">{% block leftcol %}{% endblock %}</div>
{% endif %}
And do keep in mind that you should put that kind of logic in your controllers whenever possible.

If I had to make an educated guess, I would say SF/twig are somehow confused by the rendering of two forms inside the same template/block simultaneously. I can't test these two leads right now, but here's what I would try if I were you :
Be more precise when calling your two forms in the twig view :
<form action="foo" method="post" etc...>
{{ form(form_picture) }}
{{ form_rest(form_picture) }}
{{ form_end(form_picture) }}
Symfony's form rendering is highly customizable, find all the details here if you haven't discovered them yet : How to Customize Form Rendering
Use a specific controller, or at least a separate template for at least one of your forms, ideally for each of your forms (but that's a personnal preference) :
{% block leftcol %}
{% render(controller('upload_picture') %}
// or
{% include('upload_picture_form.html.twig', with('form':'form_picture') %}
// Pardon my pseudo-code :P
{% endblock %}
{% block body %}
{{ form(form) }}
{% endblock %}

Related

How to render a form error in red color?

Using Symfony3 && PhpStorm.2016.3.2 on Ubuntu 16.04
I have a form made in Twig, here is the section that interest me:
<div class="row">
<div class="input-field col s12 validate" id="icon_telephone" type="tel">
{{ form_errors(form) }}
{% if form.vars.errors|length %}
{ form_row(
form.authorPhone,
form.authorPhone.vars|merge({'attr': {'autofocus': null}})
)
}}
{% else %}
{{ form_row(form.authorPhone) }}
{% endif %}
</div>
</div>
I would like to "colorize" the error in Red as an example, without actually using customize form rendering of Symfony which force me to create a view etc..
So I wanted to know if there is a way to actually just turn the error red without industrializing the process.
Making something red is styling so you should be doing that via CSS.
Add a css file to your form template and you can customize the rendering of your form errors like so:
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<ul>
{% for error in errors %}
<li class="error-message">{{ error.message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
And css
.error-message{
color: red;
}
Symfony doc article: https://symfony.com/doc/current/form/form_customization.html#customizing-error-output
Your final twig template would look like this:
<div class="row">
<div class="input-field col s12 validate" id="icon_telephone" type="tel">
{{ form_errors(form) }}
{% if form.vars.errors|length %}
{ form_row(
form.authorPhone,
form.authorPhone.vars|merge({'attr': {'autofocus': null}})
)
}}
{% else %}
{{ form_row(form.authorPhone) }}
{% endif %}
</div>
</div>
<link rel="stylesheet" href="{{ asset('bundles/mybundle/css/style.css') }}" />
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<ul>
{% for error in errors %}
<li class="error-message">{{ error.message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
You override the form_errors block wich is what symfony will use when you call {{ form_errors(form) }}.
If you wan't the simplest solution just inspect you errors in the browser find the right selector and apply css without customizing the form rendering in twig.
For the stylesheet, create the file under Resources/public/css then execute app/console assets:install

form_errors(form) symfony2 twig

Using Symfony2.3.4 and Twig
I bet it's an easy peasy for anybody but I just can't get it done.
I'm trying to show only one error alert with always the same message, always at the beginnig of the form, everytime there are errors regardless which field contains the invalid input.
I thought I could do it like this:
//../Professor/Resources/new.html.twig
{% extends 'AdminBundle:Default:admin.html.twig' %}
{% block content -%}
<h1>New professor</h1>
{% if form_errors(form) is not null %} {#<------------------------------------#}
<div class="alert alert-error">
<i class="glyphicon-ban-circle"></i> Message.
</div>
{% endif %}
<div class="row-fluid">
<form id="prof_create" class="form-horizontal sf_admin_form_area" action="{{ path('professor_create') }}"
method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<div class="row-fluid">
<div class="span8"></div>
</div>
<div class="form-actions">
<button class="btn btn-primary">
<i class="icon-ok"></i> {{'Create' | trans}}</button>
<a class="btn" href="{{ path('professor') }}">
<i class="icon-ban-circle"></i> {{'Cancel' | trans }}</a>
</div>
</form>
</div>
{% endblock %}
{% if form_errors(form) is not null %} is not working, meaning:
when I show the create form for the first time before entering any data in the fields,the error alert shows although there is no data in the fields.
I also tried {% if form_errors(form) %} which is also useless but the other way around, it doesn't matter if there are or not errors, the alert just won't show.
There's obviously a lot about form_errors(form) that I don't know.
Appreciate any tips or even completely different solutions.
Thanks
Have you try this ?
{% if form_errors(form.content) is not empty %}
try:
{% if form_errors(form) != '' %}
...
{% endif %}
You will also need to make sure that the error_bubbling option on all of your fields is set to true. If you dont the error only exists on the child. So the main form wouldnt have any errors even though some of the children do.
To set the error bubbling you need to do it to each field:
//Some FormType that you have created
class CustomFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('test', 'text', array('error_bubbling'=>true);
}
}
Your other option is to override the form_errors block like suggested in the accepted answer here:
{% block form_errors %}
{% spaceless %}
{% set a = false %}
{% for child in form.children %}
{% if child.get("errors") %}
{% set a = 'true' %}
{% endif %}
{% endfor %}
{% if a == true %}
<div class="alert">
{% for children in form.children %}
{{ form_errors(children) }}
{% endfor %}
</div>
{% endif %}
{% if errors|length > 0 %}
<ul>
{% for error in errors %}
{{
error.messagePluralization is null
? error.messageTemplate|trans(error.messageParameters, 'validators')
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
}}
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
try
{% if form.vars.errors|length %}
{% if not form.vars.valid %}
<div class="alert alert-error">
{{ form_errors(form) }}
</div>
{% endif %}
from https://stackoverflow.com/a/17826389/511514

Symfony2 (twig): pass array into form functions

I am trying to implement a form_function that can accept an array of forms.
FormExtension looks like this:
[...]
'form_controls_row' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
[...]
My field.html.twig would be this:
{% extends 'MopaBootstrapBundle:Form:fields.html.twig' %}
{# import "_self" as the form theme #}
{% form_theme form _self %}
{# make the form fragment customization #}
{% block form_controls_row %}
{% spaceless %}
<div class="controls controls-row">
{% for element in form %}
{{ form_row(element, _context) }}
{% endfor %}
<div>
{% endspaceless %}
{% endblock form_controls_row %}
And my form itself would look like that:
{% form_theme form 'MyBundle::fields.html.twig' %}
<div class="well">
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_controls_row([form.firstName, form.lastName]) }}
{{ form_rest(form) }}
</form>
</div>
But everytime I pass in an array ([form.firstName, form.lastName]) into form_controls_row I get an exception.
Is there any way to allow arrays?
Or is there any better solution for my purpose?

symfony2 twig referencing base widget

I am trying to customize my symfony2 form choice_widget_expanded part, and trying to reference the base (standard) for the choice_widget_collapsed as per this instruction http://symfony.com/doc/current/cookbook/form/form_customization.html
But I get an error saying
Notice: Undefined index: choice_widget_collapsed in /var/www/beta/app/cache/dev/twig/ff/8a/9f46c90c543b16e78e981aeda67b.php line 19
Here is my twig code:
{% extends '::base.html.twig' %}
{% use 'form_div_layout.html.twig' with choice_widget_collapsed as base_choice_widget_collapsed %}
{% form_theme form _self %}
{% block choice_widget %}
{% spaceless %}
{% if expanded %}
{{ block('choice_widget_expanded') }}
{% else %}
{{ block('base_choice_widget_collapsed') }}
{% endif %}
{% endspaceless %}
{% endblock choice_widget %}
{% block choice_widget_expanded %}
{% spaceless %}
<div {{ block('widget_container_attributes') }}>
{% for child in form %}
{{ form_widget(child) }}
{{ form_label(child) }}
<br />
{% endfor %}
</div>
{% endspaceless %}
{% endblock choice_widget_expanded %}
{% block contents %}
This works for me on a fresh composer install of sf 2.2.1. You should not need to clear cache for template changes to register in the dev environment, but it's worth a shot. It could also be that you need to stop and start (not restart) apache/php5-fpm if using an opcode cache.
Since you're only modifying choice_widget_expanded all you really need is:
test.html.twig
{% extends '::base.html.twig' %}
{% form_theme form _self %}
{% block choice_widget_expanded %}
{% spaceless %}
<div {{ block('widget_container_attributes') }}>
{% for child in form %}
{{ form_widget(child) }}
{{ form_label(child) }}
<br />
{% endfor %}
</div>
{% endspaceless %}
{% endblock choice_widget_expanded %}
{% block body %}
<form>
{{ form_row(form.choice) }}
</form>
{% endblock %}
testAction
/**
* #Template()
*/
public function testAction()
{
$builder = $this->createFormBuilder();
$builder->add('choice', 'choice', array(
'choices'=>array('one'=>'one', 'two'=>'two', 'three'=>'three'),
'expanded'=>true
));
$form = $builder->getForm();
return array('form'=>$form->createView());
}
Ah.. I am sure Dylans answer is correct, but for the symfony2 version I use (2.0.16) the template for that form looks a little different, the choice_widget_expanded doesn't have it's own block but it is all in this
{% block choice_widget %}
{% spaceless %}
{% if expanded %}
<div {{ block('widget_container_attributes') }}>
{% for child in form %}
{{ form_widget(child) }}
{{ form_label(child) }}
<br /> {# <-------- Here's my addition to the template #}
{% endfor %}
</div>
{% else %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none %}
<option value="">{{ empty_value|trans }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
{% set options = preferred_choices %}
{{ block('widget_choice_options') }}
{% if choices|length > 0 and separator is not none %}
<option disabled="disabled">{{ separator }}</option>
{% endif %}
{% endif %}
{% set options = choices %}
{{ block('widget_choice_options') }}
</select>
{% endif %}
{% endspaceless %}
{% endblock choice_widget %}
I Should of course have pulled the code out of my own codebase instead of grabbing the latest version off of github, doh!

Symfony2 choice_widget print only the label without markup?

Here is choice_widget template from Symfony2. I need to print the label text itself without markup, that is without calling form_label template but simply echoing the text.
The line to be sostituted is {{ form_label(child) }}. I've tried child.label but it's not working (there is no property label...). Also tried label but it prints the label of the whole widget, not the label of the current child element.
{% block choice_widget %}
{% spaceless %}
{% if expanded %}
<div {{ block('widget_container_attributes') }}>
{% for child in form %}
{{ form_widget(child) }}
{{ form_label(child) }} {# here! #}
{% endfor %}
</div>
{% else %}
{# print <select> element #}
{% endif %}
{% endspaceless %}
{% endblock choice_widget %}
Form fields' attributes are stored in their ''vars'' property :
{{ child.vars.label }}