Сущностями, переданными в поле выбора, необходимо управлять

Я создаю новый объект и привязываю его к форме. Пользователь заполняет форму и переходит на страницу предварительного просмотра. Я сохраняю ответы пользователя во время сеанса.

Проблема возникает, когда я пытаюсь перезагрузить объект из сеанса, когда пользователь возвращается к редактированию формы. Я получаю :

Ошибка: необходимо управлять сущностями, переданными в поле выбора.

Кто-нибудь знает, где я могу ошибиться? Это код для контроллеров.

public function previewdealAction(Request $request){

    $session = $this->getRequest()->getSession();
    $coupon = $session->get('coupon');
    $form = $this->createForm(new CouponType(), $coupon);

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

        //bind the posted form values
        $form->bindRequest($request);

        //once a valid form is submitted ...
        if ($form->isValid()){
           //Proceed to Previewing deal
            $file = $coupon->getImage();
            $file->upload();
            $session->set('coupon', $coupon);

            $repository = $this->getDoctrine()
            ->getRepository('FrontendUserBundle:Coupon');
            $coupons = $repository->findAll();

            return $this->render('FrontendHomeBundle:Merchant:dealpreview.html.twig', array('coupon'=>$coupon, 'coupons'=>$coupons));

        }
    }

}
public function builddealAction(Request $request){

    $em = $this->get('doctrine')->getEntityManager();
    $user = $this->container->get('security.context')->getToken()->getUser();

    //check for a coupon session variable
    $session = $this->getRequest()->getSession();

    $coupon = $session->get('coupon');

    //If coupon is not set
    if($coupon == NULL){
        $coupon = new Coupon();
        $date = new \DateTime(date("Y-m-d H:i:s"));
        $coupon->setStartdate($date);
        $coupon->setPosterid($user);
        $session->set('coupon', $coupon);
    }

    $form = $this->createForm(new CouponType(), $coupon);
    return $this->render('FrontendHomeBundle:Merchant:builddeal.html.twig', array(
        'form' => $form->createView(),
    ));
}

--

namespace Frontend\HomeBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class CouponType extends AbstractType {

public function buildForm(FormBuilder $builder, array $options) {
    $builder->add('couponname', 'text');
    $builder->add('description', 'textarea');
    $builder->add('price', 'money', array('currency' => 'USD'));
    $builder->add('originalprice', 'money', array('currency' => 'USD'));
    $builder->add('maxlimit', 'integer');
    $builder->add('maxper', 'integer');
    $builder->add('startdate', 'date', array(
        'years' => array(2011, 2012, 2013, 2014),

    ));
    $builder->add('duration', 'choice', array(
        'choices'   => array(
            '3'   => 3,
            '7'   => 7,
            '14' => 14,
            '30'   => 30,
            '60'   => 60,
            '90'   => 90,
            ),
        'expanded'  => false,
        'multiple'  => false,
        ));
    $builder->add('expirationdate', 'choice', array(
        'choices'   => array(
            '30'   => 30,
            '60'   => 60,
            '90' => 90,
            '180'   => 180,
            ),
        'expanded'  => false,
        'multiple'  => false,
        ));
    $builder->add('tip', 'integer');
    $builder->add('salestax', 'choice', array(
       'choices'   => array(
            'included'   => 'Sales tax is included and will be remitted BY YOU at the appropriate tax jurisdiction',
            'exempt'   => 'Sales tax is exempt according to seller\'s tax jurisdiction',
            'collected' => 'Sales tax will be collected BY YOU at time of deal redemption',
            ),
        'expanded'  => true,
        'multiple'  => false,
    ));
    $builder->add('signature', 'text');
    $builder->add('city', 'entity', array(
        'class' => 'Frontend\\UserBundle\\Entity\\Cities',
        'expanded' => false,
        'multiple' => false,

    ));
    $builder->add('category', 'entity', array(
        'class' => 'Frontend\\UserBundle\\Entity\\Category',
        'expanded' => false,
        'multiple' => false,
    ));
    $builder->add('address', new AddressType());
    $builder->add('image', new DocumentType());
    $builder->add('maxper', 'choice', array(
        'choices'   => array(
            '1'   => 1,
            '2'   => 2,
            '3' => 3,
            '4'   => 4,
            '5'   => 5,
            '6'   => 6,
            '7' => 7,
            '8'   => 8,
            '9'   => 9,
            '10'   => 10,
            ),
        'expanded'  => false,
        'multiple'  => false,
        ));

}

public function getDefaultOptions(array $options) {
    return array(
        'data_class' => 'Frontend\UserBundle\Entity\Coupon',
    );
}
public function getName()
{
    return 'user';
}

}

heres the coupon type class

22
задан Pierrick Rambaud 27 November 2019 в 10:50
поделиться