richelliot
richelliot

Reputation: 586

Symfony 2 form not pre-populating with pre-saved data

Im taking over a project in Symfony 2 (of which I have little knowledge) and am having problems with one of the existing forms. It should be pre-populating the form fields with existing data but is failing to do so. Can anybody give and suggestions as to why it may not be working?

Heres my code:

/**
* @Route("/admin/pressrelease/{id}")
* @Template("ImagineCorporateBundle:Admin:Pressrelease/edit.html.twig")
*/
public function editAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $repo = $em->getRepository('ImagineCorporateBundle:PressRelease');
    $pr = $repo->find($id);

    if(!$pr->getDocument())
    {
        $doc = new Document();
        $doc->setType('child');
        $doc->setTemplate('child');
        $pr->setDocument($doc);
    }

    $dateHelper = $this->get('helper.datehelper');
    $years = $dateHelper->dateRangeAction();
    $form = $this->createForm(new PressreleaseType(), array($pr , $years) );

    if($this->getRequest()->getMethod() == 'POST')
    {
        $form->bindRequest($this->getRequest());

        if($pr->getDocument())
        {
            $pr->getDocument()->setType('child');
            $pr->getDocument()->setTemplate('child');
            $pr->getDocument()->setTitle($pr->getTitle());
        }

        if($form->isValid())
        {
            $pr->upload('../web/upload/general/');
            $em->persist($pr);
            $em->persist($pr->getDocument());
            $em->flush();
            $pr->index(
                $this->get('search.lucene'),
                $this->generateUrl(
                    'imagine_corporate_pressrelease_view',
                    array('id' => $pr->getId(), 'title' => $pr->getTitle())
                )
            );

            return $this->redirect($this->generateUrl('imagine_corporate_pressrelease_admin'));
        }
    }

    return array('pressrelease' => $pr, 'form' => $form->createView());
}

And the view template:

{% extends "ImagineCorporateBundle:Admin:base.html.twig" %}

{% block heading %}Edit Press Release{% endblock %}

{% block content %}
<p>
    <a href="{{ path('imagine_cmf_attachment_new') }}">Upload Attachment</a> |
    <a href="{{ path('imagine_corporate_person_new') }}" target="_blank">New Person</a>
</p>

<form action="" method="post" {{ form_enctype(form) }}>

<div>
    {{ form_label(form.title) }}
    {{ form_errors(form.title) }}
    {{ form_widget(form.title) }}
</div>

<div>
    {{ form_label(form.author) }}
    {{ form_errors(form.author) }}
    {{ form_widget(form.author) }}
</div>

<div>
    {{ form_label(form.postdate) }}
    {{ form_errors(form.postdate) }}
    {{ form_widget(form.postdate) }}
</div>

<div>
    {{ form_label(form.imageUpload) }}
    {{ form_errors(form.imageUpload) }}
    {{ form_widget(form.imageUpload) }}
</div>

<div>
    {{ form_label(form.thumbnailUpload) }}
    {{ form_errors(form.thumbnailUpload) }}
    {{ form_widget(form.thumbnailUpload) }}
</div>

<fieldset>
    <div><input type="checkbox" class="checkallWebsites"> Check all</div>
    {{ form_label(form.websites) }}
    {{ form_errors(form.websites) }}
    {{ form_widget(form.websites) }}
</fieldset>

<fieldset>
    <div><input type="checkbox" class="checkallMagazines"> Check all</div>
    {{ form_label(form.magazines) }}
    {{ form_errors(form.magazines) }}
    {{ form_widget(form.magazines) }}
</fieldset>

 <fieldset>
    <div><input type="checkbox" class="checkallDept"> Check all</div>
    {{ form_label(form.department) }}
    {{ form_errors(form.department) }}
    {{ form_widget(form.department) }}
</fieldset>

<script>
    $(function () {
        $('.checkallWebsites').click(function () {
            $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
        });
    });

    $(function () {
        $('.checkallMagazines').click(function () {
            $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
        });
    });

    $(function () {
        $('.checkallDept').click(function () {
            $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
        });
    });
</script>


{{ form_widget(form) }}

<div id="submit">
    <input type="submit" class="addnew-submit" />
</div>
</form>

{% endblock %}

Thanks in advance!

Upvotes: 1

Views: 1919

Answers (1)

Mark
Mark

Reputation: 1754

Your issue is this line in your controller:

$form = $this->createForm(new PressreleaseType(), array($pr , $years) );

If your form is based on an entity then you can bind the entity to the form by just passing the object on it's own like so:

$form = $this->createForm(new PressreleaseType(), $pr);

If it's a more complicated form then your array needs to be key value with the form field names as the keys. For example (you may have to substitute the actual field names if they differ as we cannot see your form class):

$form = $this->createForm(
    new PressreleaseType(),
    array(
        'press_release_name' => $pr->getName(),
        'years' => $years
    )
);

EDIT: It's possible that both of those values are needed in the constructor of the form class if it has been customised so if the above doesn't help you then please add your form class code.

Upvotes: 3

Related Questions