How to use __toString () with a form in Symfony 2?

I get this error:

Objects passed to the select box must have the __toString () method (or you can also override the property option).

This is not confusing to me, since the fields that I try to do when I get this error are INT or BIGINT. What bothers me where should I use __toString () in relation to the field?

Also, what is this “property” option the error message says I can override?

Greetings

EDIT:

Here is the code that displays the form:

public function buildForm(FormBuilder $builder, array $options) { $builder->add('id'); $builder->add('title'); $builder->add('slug'); $builder->add('content'); $builder->add('keywords'); $builder->add('contenttype'); $builder->add('hits'); $builder->add('mainpage'); $builder->add('hasmainpage'); $builder->add('ismainpage'); } 

Here is the code in my Controller class that handles this:

  $pageadd = new Content(); $form = $this->createForm(new PageAdd(), $pageadd); $request = $this->getRequest(); if ($request->getMethod() == 'GET') { $form->bindRequest($request); if ($form->isValid()) { $em = $this->getDoctrine() ->getEntityManager(); $em->persist($pageadd); $em->flush(); return $this->redirect($this->generateUrl('ShoutAdminBundle_add')); } } return $this->render('ShoutAdminBundle:Default:pageadd.html.twig', array( 'form' => $form->createView() )); 

And here is the code that is in my Twig file:

  <form action="{{ path('ShoutAdminBundle_adminpageadd') }}" method="post" {{ form_enctype(form) }} class="blogger"> {{ form_errors(form) }} <p class="row"> {{ form_label(form.id, 'ID*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.id) }} {{ form_widget(form.id, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.title, 'Title*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.title) }} {{ form_widget(form.title, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.slug, 'Slug*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.slug) }} {{ form_widget(form.slug, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.content, 'Content*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.content) }} {{ form_widget(form.content, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.keywords, 'Keywords*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.keywords) }} {{ form_widget(form.keywords, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.contenttype, 'Content Type*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.contenttype) }} {{ form_widget(form.contenttype, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.hits, 'Hits*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.hits) }} {{ form_widget(form.hits, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.mainpage, 'Main Page*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.mainpage) }} {{ form_widget(form.mainpage, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.hasmainpage, 'Has Main Page*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.hasmainpage) }} {{ form_widget(form.hasmainpage, { 'attr': {'class': 'textfield'}}) }} </p> <p class="row"> {{ form_label(form.ismainpage, 'Is Main Page*', { 'attr': {'class': 'title'} }) }} {{ form_errors(form.ismainpage) }} {{ form_widget(form.ismainpage, { 'attr': {'class': 'textfield'}}) }} </p> {{ form_rest(form) }} <p class="row"> <input type="submit" value="Save This Page" class="savebutton" /> </p> </form> 
+4
source share
2 answers

I worked thanks to this post.

At first I took your advice from greg0ire, but I still got the error. However, I added:

 $builder->add('hasmainpage','entity', array('class'=>'Shout\AdminBundle\Entity\Admin', 'property'=>'id', )); 

What I understood (I did not know at the time of writing the original question) was that the error occurred because the field was tied to it, and when he tried to display the field, it could not be for the relationship.

Now it is fixed with gratitude!

+14
source

I assume that the property parameter should be populated with the name of the property you want to receive when you echo your object. I do not understand that you are not embarrassed at receiving this error and rendering only INT or BIGINT.

0
source

All Articles