Generating forms with Symfony 2.8 throws a Twig_Error_Runtime

断了今生、忘了曾经 提交于 2019-12-01 02:08:22

Looks like a regression after datetime fix in the generator bundle.

A quick solution is to revert to v2.* in your composer.json:

"sensio/generator-bundle": "^2.5",

The best solution is to fork the repo, fix the bug and create a pull request to contribute back to the community.

Since you already did all the job to isolate the bug, the fix is trivial: check if type exists in Resources/skeleton/form/FormType.php.twig. Something like

{%- if fields_mapping[field]['type'] is defined and fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}

unless the bug masks more hidden errors based on the same assumption.

I was researching a little bit and tried to debug the error.

As I mentioned above, the file form/FormType.php.twig has been changed since the version 2.8.0.

Obviously the Symfony makers wanted to enhance the forms and automatically resolve the types date, time and datetime. This happens in the line:

{%- if fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}

This should be achieved with the help of the array fields_mapping.

With some quick-and-dirty workarounds I tried to find out what is hidden inside of fields_mapping. This is the result for my model:

Task

{
    id => {
        id => 1,
        fieldName => id,
        type => integer,
        columnName => id
    },
    description => {
        fieldName => description,
        type => text,
        columnName => description
    }
}

When iterating through the fields of Task, in the last step it goes through the field tags. The expression in the if clause looks like this:

fields_mapping['tags']['type']

As we see in the previous example, there is no key tags in the fields_mapping for Task, only id and description. Since the key tags doesn't exist, the error is thrown.

I changed the concerned line in the file form/FormType.php.twig to look like this:

{%- if fields_mapping[field] is defined and fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}

Now we can use the new feature and we prevent an error by checking if the key exists in the array.

I don't know if this is a bug or there is something wrong in my particular case. Now it is already one week since the release of the versions 2.8.0 and 3.0.0, so probably many thousands users have been playing around with them. I couldn't believe that, if it is a bug, nobody would have noticed this.

EDIT:

I posted an issue on GitHub:

https://github.com/sensiolabs/SensioGeneratorBundle/issues/443

This was a bug, that has been solved in the same way, as I thought and wrote above:

https://github.com/Maff-/SensioGeneratorBundle/commit/205f64e96a94759f795271cb00fc86fb03b1fd4a

Cortez

Even if after updating fixed bundle the issue is still existing, sometimes the easiest way to solve the problem is to delete the vendor catalog, and update composer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!