开发者

Form collections in SF2

开发者 https://www.devze.com 2023-03-20 15:23 出处:网络
Offers.php /** * @var \\ZGoffers\\MainBundle\\Entity\\OfferParameter * * @ORM\\OneToMany(targetEntity=\"OfferParameter\", mappedBy=\"offer\", cascade={\"all\"})

Offers.php

/**
 * @var \ZGoffers\MainBundle\Entity\OfferParameter
 *
 * @ORM\OneToMany(targetEntity="OfferParameter", mappedBy="offer", cascade={"all"})
 */
private $parameters;

OfferParameter.php

/**
 * @var \ZGoffers\MainBundle\Entity\Offer
 *
 * @ORM\ManyToOne(targetEntity="Offer", inversedBy="offer", cascade={"all"})
 */
private $offer;

OfferType.php

class OfferType extends AbstractType
{
    public function buildForm(FormBuilder $builder,开发者_JS百科 array $options)
    {
        $builder
            ->add('advertiser')
            ->add('name')
            ->add('url', 'text', array('label' => 'URL'))
            ->add('externalUrl', 'text', array('label' => 'External URL'))
            ->add('dailyCap', 'text', array('label' => 'Daily Cap'))
            ->add('parameters', 'collection', array(
                'type' => new OfferParameterType(),
                'allow_add' => true,
                'allow_delete' => true
            ))
            ->add('active', 'choice', array(
                'choices' => array(0 => 'Disabled', 1 => 'Enabled')
            ));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'ZGOffers\MainBundle\Entity\Offer'
        );
    }
}

OfferParameterType.php

class OfferParameterType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('field')
            ->add('type', 'choice', array(
                'choices' => array(
                    '='  => 'EQUALS',
                    '>'  => 'IS GREATER THAN',
                    '>=' => 'IS GREATER THAN OR EQUALS',
                    '<'  => 'IS LESS THAN',
                    '<=' => 'IS GREATER THAN OR EQUALS'
                )
            ))
            ->add('value');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'ZGOffers\MainBundle\Entity\OfferParameter'
        );
    }
}

Form Handling

public function process()
{
    if ('POST' == $this->request->getMethod()) {
        // bind form data
        $this->form->bindRequest($this->request);

        // If form is valid
        if ($this->form->isValid() && ($offer = $this->form->getData()) instanceof Offer) {

            foreach ($offer->getParameters() as $parameter) {
                $parameter->setOffer($offer); // THIS SHOULDNT BE NEEDED
            }

            // save offer to the database
            $this->entityManager->persist($offer);
            $this->entityManager->flush();

            return true;
        }
    }

    return false;
}

My question is.... How the hell do you delete elements in form collections in SF2????

I have multiple forms that are just like this one in my project and it's really putting a halt to development :(

Thanks for all help!


You may need to do something like below :

->add('parameters', 'collection', array(
                'type' => new OfferParameterType(),
                'allow_add' => true,
                'allow_delete' => true,
                'attr'=>array('style'=>'display:none;')
            ))

That makes your whole OfferParameterType collection hidden.

Or in OfferParameterType apply same thing for each field like below :

->add('field',null, array('attr'=>array('style'=>'display:none;')))
0

精彩评论

暂无评论...
验证码 换一张
取 消