简体   繁体   中英

How do I pass a value to Form Builder in Symfony2?

I'm using a Form Builder to create a form for use in an application. In the form, the user needs to be able to select a list of events that they are associated with. However, I'm unable to figure out how exactly I can pass the user's ID to the form builder?

My code is like this at the moment:

EvType.php

<?php
// src/Acme/MembersBundle/Form/Type/EvType.php
// This is to handle forms for the Members Form
namespace Acme\MembersBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class EvType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $centre = $this->centre;

        $builder->add('id', 'integer', array('required'=>false));
        $builder->add('list','entity', array('class'=>'Acme\InstructorBundle\Entity\MapLists', 'property'=>'name',
                                                        'query_builder' => function(EntityRepository $br) {
                                                            return $br->createQueryBuilder('ml')
                                                                ->where('ml.user = :user')
                                                                ->setParameter('user','1' );
                                                        }));
        $builder->add('eventHorse', 'text', array('required'=>false));
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'      => 'Acme\InstructorBundle\Entity\MapListCentreMembers',
            'csrf_protection' => false,
            'csrf_field_name' => '_token',
            // a unique key to help generate the secret token
            'intention'       => 'task_item',
        ));
    }

}

->setParameter('user','1' ); is where I want to be able to pass the User's ID from the form. For now, I've statically assigned the user ID.

DefaultController.php

// User ID
$userid = $mem['userID'];

// Get Tests from Entity for Form use
$memberEV = $dm->getRepository('InstructorBundle:MapListMembers')->find($memberint);

// Generate Form to edit Tests & Achievements
$ev = $this->createForm( new EvType(), $memberEV);

you can simply pass a value in the __construct.

See below:

EvType.php

class EvType extends AbstractType
{
    private $user;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
         $user = $this->user;
         ....
    }

DefaultController.php

$ev = $this->createForm( new EvType($user), $memberEV);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM