How to pass entity_manager from controller to form class as option? error: The required option "entity_manager" is missing
How to pass entity_manager from controller to form class as option? error: The required option "entity_manager" is missing
我想在我的表单中创建一个动态下拉字段。下拉列表中的选项取自 table 的 'name' 字段。我已经使用表单 class 创建了表单,并将 entity_manager 从我的控制器传递到表单 class。
FamilyType - 形式 class
namespace App\Form;
use App\Entity\Family;
use App\Form\AddressType;
use Symfony\Component\Form\AbstractType;
use App\Entity\Vendor;
//for radio buttons
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\Request;
class FamilyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entityManager = $options['entity_manager'];
$list = $entityManager->getRepository(Vendor::class)->getNameList();
$arr = [];
foreach($list as $key=>$value){
foreach($value as $k=> $v){
$arr[$v] = $v;
}
}
$builder
->add('name', TextType::class, [
'attr' => [
'placeholder' => 'Enter Name',
],
])
->add('contact', TelType::class, [
'attr' => [
'placeholder' => 'Enter COntact number',
],
])
->add('gender', ChoiceType::class, [
'choices' => $arr,
'choices_as_values' => true, 'multiple' => false, 'expanded' => true,
]
)
->add('address', AddressType::class, [
//To get validation error for embeded form
'constraints' => array(new Assert\Valid()),
])
->add('favorite_food', ChoiceType::class, [
'choices' => $arr,
])
->add('Save',SubmitType::class,[
'attr' => [
'class' => 'btn btn-success '
]
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Family::class,
]);
$resolver->setRequired('entity_manager');
}
}
家庭控制器:
public function familyFoodForm(Request $request)
{
$entityManager = $this->getDoctrine()->getManager();
$family = new Family();
$f_form = $this->createForm(FamilyType::class, $family, [
'entity_manager' => $entityManager,
]);
$f_form->handleRequest($request);
if ($f_form->isSubmitted()) {
if ($f_form->isValid()) {
$address = $family->getAddress();
$entityManager->persist($address);
$entityManager->flush();
$entityManager->persist($family);
$entityManager->flush();
$this->addFlash('success', 'Welcome!! Family is added.');
}
}
return $this->render('family/index.html.twig', [
'form' => $f_form->createView(),
]);
}
上面的代码给我错误:
缺少必需的选项 "entity_manager"。
我是 symfony 的新手,我是通过以下方式创建的:
https://symfony.com/doc/current/form/form_dependencies.html
此处 AddressType 表单嵌入到 FamilyType 中,同时需要将 'entity_manager'(如问题中所述)也传递给嵌入表单 class。
因此,在您的 FamilyType - 表单 class
中
->add('address', AddressType::class, [
//To get validation error for embeded form
'constraints' => array(new Assert\Valid()),
'entity_manager' => $options['entity_manager']
])
现在,entity_manager 选项已传递到您的表单。干杯。
我想在我的表单中创建一个动态下拉字段。下拉列表中的选项取自 table 的 'name' 字段。我已经使用表单 class 创建了表单,并将 entity_manager 从我的控制器传递到表单 class。
FamilyType - 形式 class
namespace App\Form;
use App\Entity\Family;
use App\Form\AddressType;
use Symfony\Component\Form\AbstractType;
use App\Entity\Vendor;
//for radio buttons
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\Request;
class FamilyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entityManager = $options['entity_manager'];
$list = $entityManager->getRepository(Vendor::class)->getNameList();
$arr = [];
foreach($list as $key=>$value){
foreach($value as $k=> $v){
$arr[$v] = $v;
}
}
$builder
->add('name', TextType::class, [
'attr' => [
'placeholder' => 'Enter Name',
],
])
->add('contact', TelType::class, [
'attr' => [
'placeholder' => 'Enter COntact number',
],
])
->add('gender', ChoiceType::class, [
'choices' => $arr,
'choices_as_values' => true, 'multiple' => false, 'expanded' => true,
]
)
->add('address', AddressType::class, [
//To get validation error for embeded form
'constraints' => array(new Assert\Valid()),
])
->add('favorite_food', ChoiceType::class, [
'choices' => $arr,
])
->add('Save',SubmitType::class,[
'attr' => [
'class' => 'btn btn-success '
]
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Family::class,
]);
$resolver->setRequired('entity_manager');
}
}
家庭控制器:
public function familyFoodForm(Request $request)
{
$entityManager = $this->getDoctrine()->getManager();
$family = new Family();
$f_form = $this->createForm(FamilyType::class, $family, [
'entity_manager' => $entityManager,
]);
$f_form->handleRequest($request);
if ($f_form->isSubmitted()) {
if ($f_form->isValid()) {
$address = $family->getAddress();
$entityManager->persist($address);
$entityManager->flush();
$entityManager->persist($family);
$entityManager->flush();
$this->addFlash('success', 'Welcome!! Family is added.');
}
}
return $this->render('family/index.html.twig', [
'form' => $f_form->createView(),
]);
}
上面的代码给我错误: 缺少必需的选项 "entity_manager"。
我是 symfony 的新手,我是通过以下方式创建的: https://symfony.com/doc/current/form/form_dependencies.html
此处 AddressType 表单嵌入到 FamilyType 中,同时需要将 'entity_manager'(如问题中所述)也传递给嵌入表单 class。
因此,在您的 FamilyType - 表单 class
中->add('address', AddressType::class, [
//To get validation error for embeded form
'constraints' => array(new Assert\Valid()),
'entity_manager' => $options['entity_manager']
])
现在,entity_manager 选项已传递到您的表单。干杯。