Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Use __toString() with a form in Symfony 2?

Tags:

symfony

I get this error:

Entities passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).

This isn't confusing me, as the fields that I try to render when I get this error are either INT's or BIGINT's. What is confusing me is where I'm supposed to use __toString() in relation to the field?

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

Cheers

EDIT:

Here's the code that renders 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's 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's 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>
like image 854
mickburkejnr Avatar asked Dec 07 '22 19:12

mickburkejnr


1 Answers

I worked it out, with thanks to this blog post.

I took your advice firstly greg0ire, but I still received the error. What I did however was I added:

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

What I realised (I didn't know at the time of writing the original question) was that the error occurred because the field had a relationship attached to it, and when it tried to render the field it couldn't because of the relationship.

It's now fixed thankfully!

like image 157
mickburkejnr Avatar answered Dec 10 '22 23:12

mickburkejnr