A
A
ak_wi2019-11-29 23:44:07
symfony
ak_wi, 2019-11-29 23:44:07

How to fix bug in Symfony 5.0 An exception has been thrown during the rendering of a template?

I decided to create a simple application on 5.0.0:

symfony new my_project_name --full
composer dump-env dev

echo "7.2" > .php-version(the server did not work on 7.0)
php bin/console make:entity(added one field - sting for example)
php bin/console make:crud
And after that "An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class App\Entity\Color could not be converted to string")."

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2019-11-29
@ak_wi

The problem is in the maker-bundle (not tied to the Symfony version). It doesn't create a __toString magic method to display entities. You need to add it yourself:

public function __toString()
{
    return $this->name;
}

But in your case, the problem is that the entity is called Color and a form called ColorType is created for it. It uses a form template to render it.
\Symfony\Component\Form\Extension\Core\Type\ColorType
in which, apparently, your essence is cast into a string.
Add a \App\Form\ColorTypemethod in the class getBlockPrefixand everything will work:
public function getBlockPrefix()
{
    return 'app_color';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question