0

I having some troubles with symfony forms. I have a boolean variable in my entity. I cant find a way to set that value in my form when i'm editing it.

Entity

namespace AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="pages")
 */
class Page
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="text")
     */
    protected $slug;

    /**
     * @ORM\Column(type="text")
     */
    protected $title;

    /**
     * @ORM\Column(type="text")
     */
    protected $content;

    /**
     * @ORM\Column(name="creation_date", type="datetime")
     */
    protected $creationDate;

    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    protected $isActive = true;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set slug
     *
     * @param string $slug
     *
     * @return Page
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get slug
     *
     * @return string
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set title
     *
     * @param string $title
     *
     * @return Page
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set content
     *
     * @param string $content
     *
     * @return Page
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return string
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Set creationDate
     *
     * @param \DateTime $creationDate
     *
     * @return Page
     */
    public function setCreationDate($creationDate)
    {
        $this->creationDate = $creationDate;

        return $this;
    }

    /**
     * Get creationDate
     *
     * @return \DateTime
     */
    public function getCreationDate()
    {
        return $this->creationDate;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     *
     * @return Page
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean
     */
    public function getIsActive()
    {
        return $this->isActive;
    }
}

Controller function

public function editAction(Request $request, $pageId)
    {   
        $em = $this->getDoctrine()->getManager();

    $page = $em->getRepository('AdminBundle:Page')->find($pageId);

        $form = $this->createForm('app_page', $page);

        $form->handleRequest($request);

    if($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($page);
            $em->flush();

            return $this->redirectToRoute('admin_pages');
        }

    return $this->render('AdminBundle::editPage.html.twig', array('page' => $page, 'form' => $form->createView()));
    }

PageFormType

namespace AdminBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class PageFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder->add('title', 'text');
        $builder->add('slug', 'text');
        $builder->add('isActive', 'choice', array('choices' => array(
            'true' => 'Active',
            'false' => 'Inactive'
        )));
        $builder->add('content', 'textarea', array('required' => false));
    }

    public function getName()
    {
        return 'app_page';
    }
}

As you can see i wrote my self in FormType class what choices it has. And now it is always displaying "Active" instead of displaying Entity's value. How should i do that?

Thanks!

4
  • 1
    your entity isActive value probably isn't "true" or "false" as a string. In your formType, your choice field is looking for those values as string. Commented Feb 18, 2016 at 13:35
  • it is not string. what should i write in choices then? Commented Feb 18, 2016 at 13:52
  • A boolean value is represented as a check button, you can see it with $builder->add('isActive'); , if you want a choice then you need to manage the conversion between the boolean value and the "true" "false" strings. Commented Feb 18, 2016 at 14:06
  • not sure it will work, but it could be a quick fix : try to replace 'true' by 1 and 'false' by 0 Commented Feb 18, 2016 at 14:13

3 Answers 3

1

Try to change your isActive in the Entity like

protected $isActive ;

and if you want to make a default true you can make it at the construct like :

public function __construct() {
    $this->setIsActive = true ;
}
Sign up to request clarification or add additional context in comments.

Comments

1
 $builder->add('isActive', ChoiceType::class, [
        'choices' => [
            'Active' => true,
            'Inactive' => false
        ],
        'choices_as_values' => true,
        'multiple' => true,
        'expanded' => true,
        'data' => array(true)
    ]);

Comments

0

A 2021 Solution: setting form choice default value (data) from entity

Sometimes your choice field values exist in another entity table, which means you cannot populate them using default form data_class. You only get the string value that saved in the form entity, resulting to error: ...must be an object or null, string given...

protected EntityManagerInterface $em;

public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}



// In your form
->add('membershipType', EntityType::class, [
            'class' => MembershipType::class,
            'label' => 'Enter membership type here',
            'data' => $this->em->getRepository(MembershipType::class)->findOneBy([
                'name' => $your_value_here
            ])
        ])

Comments

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.