3

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) }}
7
  • There is no error in your visible code. Are you sure it is happening because of form? What is your twig template code? Commented Feb 26, 2015 at 11:28
  • I edited to display the full template code. the strange thing is that with other entities it works (same template and almost same code) Commented Feb 26, 2015 at 11:32
  • Have you set the data_class attribute 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 appen Commented Feb 26, 2015 at 11:33
  • ho I havent but I have other entities working in the same way. do you have an example on how to do that? thanks. Commented Feb 26, 2015 at 11:36
  • Your solution with form_start() does not answer the question "why?", though. Commented Feb 26, 2015 at 13:16

2 Answers 2

1

I think I've found an answer to your question here.

the method inherited from AbstractType will create the name according to the class name which will lead to search. But this will cause issues when rendering as there is a block to render the type search but for the core type. You should set the name explicitly by using a name not used already (a good way is to prefix it by the alias of the bundle)

So the issue could be in the name search itself. Try specifying different name then.

Sign up to request clarification or add additional context in comments.

Comments

0

Every form needs to know the name of the class that holds the underlying data, as described here

You should add the following method to the SearchType class:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MyApplicationBundle\Entity\Search',
    ));
}

and remember to add the following use method:

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Hope this help

2 Comments

unfortunately this didn't work. I found a workaround and I updated my question. thanks anyway
This is more of a recomendation in case of complex structure. Code above should work without it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.