M
M
Maxim Savichev2015-09-16 12:57:45
symfony
Maxim Savichev, 2015-09-16 12:57:45

How to pass values ​​to form symfony2?

Good afternoon, I can't figure out how to create a separate symfony form.
Here is the actual code for a regular form without creating a Form file in the controller:

//Находим id_user
        $id_user= $this->get('security.context')->getToken()->getUser()->getId();
        $ems_select = $this->getDoctrine()->getEntityManager();

        //Задаем default values
        $donut = new Donut();
        $donut->setIdServer(1);
        $donut->setSum(1);
        $donut->setDate(new \DateTime);

        //Создаем форму
        $form = $this->createFormBuilder($donut)
            ->add('sum','integer', array('label' => 'Количество шариков'))
            ->add('id_server', 'entity', array(
                'label' => 'Выберите ваш сервер',
                'attr' => array('class' => 'browser-default'),
                'required' => false,
                'class'  => 'M4MinecraftBundle:Mc_server',
                'query_builder' => function(EntityRepository $ems_select) use($id_user){
                    return $ems_select->createQueryBuilder('s')
                        ->where('s.id_user IN (:id_user)')
                        ->setParameter('id_user', $id_user);},
                'property'=> 'name'
            ))
            ->getForm();

Everything works fine in the controller. But when switching to a separate file, several questions arise at once:
1. I don't know how to pass $id_user to the form;
2. I don't know how to query the database using $id_user;
2.I don't know how to set default values ​​in the form;
Errors:
1. If you paste
$id_user= $this->get('security.context')->getToken()->getUser()->getId();
in
public function buildForm(FormBuilderInterface $builder, array $options)
{}

Then an error occurs:
Attempted to call an undefined method named "get" of class "\FormType".
Did you mean to call eg "getName" or "getParent"?
And it's impossible to pass through $options array, it doesn't understand the identifier.
2.When turned on
$ems_select = $this->getDoctrine()->getEntityManager();

gives
Attempted to call an undefined method named "getDoctrine" of class "M4\MinecraftBundle\Form\DonutType".
Tried to write in FormType
use Doctrine\ORM\EntityRepository;

Does not help.
How to make sure that this form can be created in a separate file and there are no such errors?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-09-16
@MaksimSa

class ExampleType extends AbstractType
{

    public function buildForm(FormBuilder $builder, array $options)
    {
        $id_user = $option['id_user'];

        $builder->add('id_server', 'entity', array(
                // ...
                'query_builder' => function(EntityRepository $ems_select) use($id_user) {
                },
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            // ..
            'id_user' => '',
        ));
    }

    // ...
}


class ExampleController extends Controller
{
    public function main()
    {
        $form = $this->createForm(new ExampleType(), $entity, [
            // ..
            'id_user' => 0,
        ]);

        // ..
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question