I have this simply controller:
class SearchController extends Controller{
public function indexAction(Request $request)
{
$search = new Search();
$form = $this->createForm(new SearchType(), $search);
$form->handleRequest($request);
if ($form->isValid()) {
return $this->redirect($this->generateUrl('search'));
}
return $this->render('MyApplicationBundle:Search:index.html.twig', array(
'form' => $form->createView(),
));
}
}
this is the search entity:
class Search {
protected $query;
public function setQuery($query)
{
$this->query = $query;
}
public function getQuery()
{
return $this->query;
}
}
and this is my form:
class SearchType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('query', 'text')
->add('search', 'submit');
}
public function getName()
{
return 'search';
}
}
unfortunately when trying to render the form
{% extends '::base.html.twig' %}
{% block body %}
{{ form(form) }}
{% endblock %}
I got this error:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class My\ApplicationBundle\Entity\Search could not be converted to string in
BTW, the rendering of just HTML works fine.
any idea? thanks for your help
SOLVED: I found a workaround changing how to render the form:
{{ form_start(form, {'attr': {'class': 'form-search'}}) }}
{{ form_widget(form.query, {'attr': {'class': 'form-control search-query'}}) }}
{{ form_end(form) }}
data_classattribute to the formtype? As described in the section Setting the data_class of the doc. BTW you can add the__toString()method to the entity to see what appenform_start()does not answer the question "why?", though.