CreateIT
CreateIT
BLOG

Symfony2 Broken selects PHP7.4

TAGS:

Symfony2 Broken selects PHP7.4

SHARE

CHALLENGE:
In Symfony2 System – suddenly some selects are broken. Twig template HTML is rendering incorrect markup: empty space between selects and id paramater is missing.
SOLUTION:
Overwrite Twig template or downgrade PHP version
ADDITIONAL COMMENT:
Turns out Server Administrator changed PHP version for entire server and this was direct cause of the bug.

Since the beginning, Symfony has not only been associated with a tool, but also a community and a philosophy combined into a single, harmonious entity.
By default, Symfony2 uses PHP 5.6. However, after the upgrade to PHP 7.0 or PHP 7.1 some features stop functioning properly. This short article will provide you with hints on how to fix a system that was written in Symphony2.x so that it functions properly with PHP 7.x .
Bugs are an inevitable aspect of all types of tools that are constantly evolving, and so is the case with Symfony2 php7. Some of the errors one may encounter include the Symfony twig error/Symfony2 twig rendering error and the Symfony2 rendering form error. If you are struggling with these two issues – don’t worry, you will find possible solutions below.

1. System based on Symfony2 was working correctly, but suddenly some select tags started to render incorrectly.

There weren’t any recent code updates on the production server. Page source code investigation showed that selectid=”someid” is rendered without an empty space before id param.

<!-- /section:custom/widget-box.options.collapsed -->
<input type="text"id="organisation_filter_name" name="organisation_filter[name]" placeholder="Organisation name"/>
        <selectid
        ="organisation_filter_country" name="organisation_filter[country]">
        <option value="">Country</option>
        <option value="AF">Afghanistan</option>
        <option value="AL">Albania</option>
        <option value="DZ">Algeria</option>
        (...)
        <option value="YE">Yemen</option>
        <option value="ZM">Zambia</option>
        <option value="ZW">Zimbabwe</option>
        <option value="AX">Åland Islands</option>
        </select>

2.Let’s use Symfony Debug bar ( url: /app_dev.php/ ) and start debugging the code.

A. looking for a proper template.

/src/MYCODE/Admin/OrganisationBundle/Resources/views/Organisation/_filters_form.html.twig
We found the template, but form_widget is a core Symfony function, we have to search in the vendors.

<form class="form-inline" method="post">

    {{ form_errors(filters_form) }}

    {{ form_widget(filters_form.name, {'attr': {'placeholder': 'Organisation name'}}) }}
    {{ form_widget(filters_form.country) }}
    {{ form_widget(filters_form.region) }}
    {{ form_widget(filters_form.is_member) }}
    {{ form_widget(filters_form.type) }}
    {{ form_widget(filters_form.membership_type) }}
    {{ form_widget(filters_form._token) }}

    {{ form_widget(filters_form) }}

    <button name="filter" class="btn btn-xs btn-info"><i class="ace-icon fa fa-search"></i> Filter</button>
    <button name="reset" value="reset" class="btn btn-xs btn-warning"><i class="ace-icon fa fa-eraser"></i> Reset</button>
</form>

 

B. Finally we have found the root of the issue:

/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Block widget_attributes, for some reason, is not adding an empty space before id.

{%- block widget_attributes -%}
    id="{{ id }}" name="{{ full_name }}"
    {%- if read_only %} readonly="readonly"{% endif -%}
    {%- if disabled %} disabled="disabled"{% endif -%}
    {%- if required %} required="required"{% endif -%}
    {%- for attrname, attrvalue in attr -%}
        {{- " " -}}
        {%- if attrname in ['placeholder', 'title'] -%}
            {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
        {%- elseif attrvalue is sameas(true) -%}
            {{- attrname }}="{{ attrname }}"
        {%- elseif attrvalue is not sameas(false) -%}
            {{- attrname }}="{{ attrvalue }}"
        {%- endif -%}
    {%- endfor -%}
{%- endblock widget_attributes -%}

 

3. Possible solutions:

I. overwrite form_div_layout.html.twig and add the “empty space” fix.

More info: https://stackoverflow.com/a/12243140

{%- block widget_attributes -%}
    {{- " " -}}id="{{ id }}" name="{{ full_name }}"
    {%- if read_only %} readonly="readonly"{% endif -%}
    {%- if disabled %} disabled="disabled"{% endif -%}
    {%- if required %} required="required"{% endif -%}
    
    (..)
{%- endblock widget_attributes -%}
II. change PHP version from 7.4 to 7.1

( It turns out with PHP7.1 enabled – the form templates are working properly )

 

___

About the article Author:

Maciej Brzeziński

Techleader of createIT, working in the IT sector for over 14 years. Innovator, author, and owner of an e-shop that he has been continuously developing for over 10 years. Currently, with his team of over 20 developers, he supervises all kinds of e-commerce projects of varied structure, characteristics, and size: from simple on-line shops for retail clients to advanced B2B platforms with support of multiple shopping carts, multiple languages, and integration with external systems. His work also involves modifying and expanding the existing platforms with millions of products in their databases, platforms that support variable pricing (several variants of the same product), dropshipping modules, integration with storage systems, integration with courier delivery services, and the last but not least, the optimization of scripts. By participating in a multitude of projects, he had the opportunity to “test out” all kinds of platforms (open-source, SaaS – Cloud-Hosted eCommerce Platforms) and dedicated platforms.

About us:

Hi, we are createIT!

As a passionate group of professionals with a love for Web and mobile technologies, we have been successfully serving our clients for the past 15 years. Through these years we have continuously striven to create the best IT solutions our clients seek.

Our 70+ developer team creates cutting-edge complex B2B and B2C web systems and apps as well as delivers top-notch direct outsourcing services.

We work in multiple frameworks but we hold special feelings and are experts in PHP (Symfony, OXID, WordPress), JavaScript (React, AngularJS), Flutter and Blockchain. We do enjoy working in the scalable cloud environment like AWS.

We are located in Warsaw, Poland, in the heart of Europe. Concentrated on delivering durable web solutions of high integrity, we do not always try to be the cheapest. However, thanks to our location you will be surprised how reasonable our prices are.

Fluent in English we offer services worldwide. Among others, in countries such as the U.S.A., Great Britain, Germany, Australia, Sweden and Poland.

Need help?

  • Looking for support from experienced programmers?

  • Need to fix a bug in the code?

  • Want to customize your webste/application?

1 comment

Hi

Just had the same issues on an older Symfony project when upgrading to php 7.4 and found that the issue is with some code in Twig that got fixed in recent updates:
https://github.com/twigphp/Twig/pull/3004/commits/1fb0f9701d8443083495cd2645e8a0c45d54c34d

Updating Twig to a more recent version fixed it for us:
composer require “twig/twig:^2.0”

Fixing it on a single template would work but you’d risk it being an issue on some other obscure template or at some point in the future, whereas updating Twig you know you’re safe for any other potential issues.

ADD COMMENT

Your email address will not be published. Required fields are marked *

createIT Contact