我想在createForm函数中传递语言。 在我的Person Entity中,我有3个外键给其他实体。在这个实体中,我有一个法语区和另一个荷兰语区。 所以,我想在构建表单时传递语言来选择好的字段。
我的控制器:
namespace RipliBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use RipliBundle\Entity\Person;
use RipliBundle\Form\PersonType;
use Symfony\Component\HttpFoundation\Request;
class PersonController extends Controller
{
//view a list of persons
public function viewPersonAction()
{
$repository = $this->getDoctrine()->getManager();
$person = $repository->getRepository('RipliBundle:Person')->findAll();
return $this->render('RipliBundle:Person:viewPerson.html.twig', array(
'person' => $person));
}
/*
* add a person
* get the nationality entity of Belgium in db
* create a new person and set the nationality by default
* call the function to generate and view the form
* if form submitted, persist in db
*/
public function addPersonAction(Request $request) {
$repository = $this->getDoctrine()->getManager()
->getRepository('RipliBundle:Nationality');
$nationality = $repository->find(42);
$person = new Person();
$person->setNationality($nationality);
return $this->formManagement($person, 'manage', $request);
}
/*
* modify a person
* get the person datas in db
* call the function to generate and view the form
* if form submitted, persist in db
*/
public function modifyPersonAction($id, Request $request)
{
$repository = $this->getDoctrine()->getManager();
$person = $repository->getRepository('RipliBundle:Person')->find($id);
return $this->formManagement($person, 'manage', $request);
}
/*
* generate and view the form
* if form submitted, persist in db
*/
private function formManagement($person, $actionView, Request $request)
{
$form = $this->createForm(PersonType::class, $person, array('lang' => 'N'));
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($person);
$em->flush();
$this->addFlash('notice', 'Person saved successfully');
}
return $this->render('RipliBundle:Person:'.$actionView.'Person.html.twig', array(
'form' => $form->createView()));
}
}
我的PersonType
namespace RipliBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class PersonType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('firstName')
->add('birthDate', BirthdayType::class,array(
'years' => range(date('Y'), 1930),
'format' => 'dd / MM / yyyy'
))
->add('nationality',EntityType::class,array(
'class' => 'RipliBundle:Nationality',
'choice_label' => 'nationalityF',
'label' => 'Nationality',
'query_builder' => function(\RipliBundle\Repository\NationalityRepository $er){
return $er->listNationalityActive();
}
))
->add('sex',EntityType::class,array(
'class' => 'RipliBundle:Sex',
'choice_label' => 'sexF',
'label' => 'Sex',
'placeholder' => '- - - -',
'query_builder' => function(\RipliBundle\Repository\SexRepository $er){
return $er->listSexActif();
}
))
->add('language',EntityType::class,array(
'class' => 'RipliBundle:LanguagePerson',
'choice_label' => 'languageF',
'label' => 'Language',
'placeholder' => '- - - -',
'query_builder' => function(\RipliBundle\Repository\LanguagePersonRepository $er){
return $er->listLanguageActive();
}
))
->add('save', SubmitType::class, array('label' => 'Save'))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'RipliBundle\Entity\Person'
));
}
}
我想做
namespace RipliBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class PersonType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('firstName')
->add('birthDate', BirthdayType::class,array(
'years' => range(date('Y'), 1930),
'format' => 'dd / MM / yyyy'
))
->add('nationality',EntityType::class,array(
'class' => 'RipliBundle:Nationality',
'choice_label' => 'nationality'.$option['lang'],
'label' => 'Nationality',
'query_builder' => function(\RipliBundle\Repository\NationalityRepository $er){
return $er->listNationalityActive($option['lang']);
}
))
->add('sex',EntityType::class,array(
'class' => 'RipliBundle:Sex',
'choice_label' => 'sex'.$option['lang'],
'label' => 'Sex',
'placeholder' => '- - - -',
'query_builder' => function(\RipliBundle\Repository\SexRepository $er){
return $er->listSexActif($option['lang']);
}
))
->add('language',EntityType::class,array(
'class' => 'RipliBundle:LanguagePerson',
'choice_label' => 'language'.$option['lang'],
'label' => 'Language',
'placeholder' => '- - - -',
'query_builder' => function(\RipliBundle\Repository\LanguagePersonRepository $er){
return $er->listLanguageActive($option['lang']);
}
))
->add('save', SubmitType::class, array('label' => 'Save'))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'RipliBundle\Entity\Person'
));
}
}
那么,我该怎么做?
由于
答案 0 :(得分:2)
我是解决方案
在configureOptions函数中,我添加了' lang' => ' F'那没关系
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'RipliBundle\Entity\Person',
'lang' => 'F'
));
}