0

I have an entity

class Icon
{
...

     /**
     * @var array $padding
     *
     * @ORM\Column(name="padding", type="array")
     */
    private $padding;

    /**
     * Set padding
     *
     * @param array $padding
     * @return Icon
     */
    public function setPadding($padding)
    {
        $this->padding = $padding;
        return $this;
    }

    /**
     * Get padding
     *
     * @return array
     */
    public function getPadding()
    {
        return $this->padding;
    }

...
}

, an icon form

<?php

namespace Acme\ExBundle\Form;

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

class IconPaddingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('padding', new PaddingType())
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\ExBundle\Entity\Icon'
        ));
    }

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

and a padding form

<?php

namespace Acme\ExBundle\Form;

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

class PaddingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('top', 'integer', array('data' => 0))
            ->add('right', 'integer', array('data' => 0))
            ->add('bottom', 'integer', array('data' => 0))
            ->add('left', 'integer', array('data' => 0))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
    }

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

If I create or update icon with IconType

and fill paddings, icon saves as I expect.

But after this when I edit icon again,

PaddingForm does not use icon.padding values

to populate the form ( top, left, right, bottom are '0' again).

How do you fill embed PaddingForm with icon.padding values?

1
  • Post your controller code please. Commented Dec 12, 2012 at 22:15

1 Answer 1

1

I think that data overrides the data passed when form is constructed.

I suggest setting 0 values within your entity as defaults (either in property directly or in __construct) and removing those data properties from form.

class IconPadding{
    ....
    public function __construct(){
        $this->left = $this->rigth = $this->bottom = $this->top = 0;
    }
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

yep you're right $builder->add('top', 'integer', array('data' => 0)) overwrites icon.padding values i just remove them and set defaults in Icon Entity ` /** * @var array $padding * * @ORM\Column(name="padding", type="array") */ private $padding = array( 'top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0 ); `

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.