Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - Sluggable not set when using form

I am trying to use the Sluggable behaviour from the Doctrine Extensions bundle:

http://gediminasm.org/article/sluggable-behavior-extension-for-doctrine-2

I have set up a sluggable field in my entity using annotation but the value does not get set when I use a form to create an instance, which causes the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'slug' cannot be null

Here's the code from my controller:

        $form = $this->createFormBuilder($section)
                        ->add('title', 'text')
                        ->getForm();

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

            if ($form->isValid()) {

                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($section);
                $em->flush();

                if (empty($id)) {
                    return $this->redirect($this->generateUrl('ContentBundle_section_new'));
                }
                else {
                    return $this->redirect($this->generateUrl('ContentBundle_section_edit', array('id' => $id)));
                }

            }
        }

And the sluggable field definition in the Entity class:

    /**
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(length=128, unique=true)
     */
    private $slug;

If I add the slug field to the formbuilder and set a value manually, it works OK but obviously I don't want to be messing around with this.

Can anyone help?

Thanks

like image 680
Dan Avatar asked Jul 16 '12 17:07

Dan


People also ask

What is Symfony and how does it work?

Symfony is very powerful when it comes to abstracting your data and this makes the form building very easy. If, however, you need some ‘on the fly’ processing of a form that does not save anything, you can build the form right into the View and then catch the submitted values in the controller.

How do I submit a form in Symfony?

You've also assigned each a form type (e.g. TextType and DateType ), represented by its fully qualified class name. Finally, you added a submit button with a custom label for submitting the form to the server. Symfony recommends to put as little logic as possible in controllers.

How to check if a form is valid in Symfony?

In Symfony, the question isn't whether the "form" is valid, but whether or not the underlying object ($task in this example) is valid after the form has applied the submitted data to it. Calling $form->isValid() is a shortcut that asks the $task object whether or not it has valid data.

Why use Symfony controller instead of classes?

Symfony recommends to put as little logic as possible in controllers. That's why it's better to move complex forms to dedicated classes instead of defining them in controller actions. Besides, forms defined in classes can be reused in multiple actions and services.


1 Answers

Got it.

I had forgotten to add the following line to the config.yml file:

sluggable: true

So it should read something like:

stof_doctrine_extensions:
    default_locale: en
    translation_fallback: true
    orm:
        default:
            tree: true
            timestampable: true
            sluggable: true
like image 162
Dan Avatar answered Oct 21 '22 00:10

Dan