Answer the question
In order to leave comments, you need to log in
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();
$id_user= $this->get('security.context')->getToken()->getUser()->getId();in
public function buildForm(FormBuilderInterface $builder, array $options)
{}
$ems_select = $this->getDoctrine()->getEntityManager();
use Doctrine\ORM\EntityRepository;
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question