[英]symfony4 inject parameter in form
I have a Business Entity and a BusinessObject Entity, and I would like to link the BusinessObject to the current Business when I create a new BusinessObject. 我有一个业务实体和一个BusinessObject实体,当我创建一个新的BusinessObject时,我想将BusinessObject链接到当前的业务。 For example, if my route is
business/{id}/object/new
, I would like to have the object related with the Business (thanks to the id). 例如,如果我的路线是
business/{id}/object/new
,那么我希望具有与Business相关的对象(由于id)。
In my BusinessObject Controller, I managed to use @ParamConverter
to get the Business id. 在我的BusinessObject Controller中,我设法使用
@ParamConverter
来获取业务ID。 In my BusinessObject Form, I put an HiddenType to my business entry because I don't want it to appear, and set data
to business_ID. 在我的BusinessObject Form中,我将HiddenType放到我的业务条目中,因为我不希望它出现,并将
data
设置为business_ID。 I struggle in configureOptions to get the business ID, I can't figure out how to get the business id from here. 我在configureOptions中很难获得业务ID,我不知道如何从此处获取业务ID。
BusinessObject Controller (route new) : BusinessObject Controller(新路由) :
/**
* @Route("/{post_id}/new", name="business_object_new", methods="GET|POST")
* @ParamConverter("business", options={"id" = "post_id"})
*/
public function new(Request $request,Business $business): Response
{
$businessObject = new BusinessObject();
$businessID = $business->getId();
$form = $this->createForm(BusinessObjectType::class, $businessObject,array(
'business_ID'=>$businessID,
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($businessObject);
$em->flush();
return $this->redirectToRoute('business_object_index');
}
return $this->render('business_object/new.html.twig', [
'business_object' => $businessObject,
'business'=>$business,
'form' => $form->createView(),
]);
}
BusinessObjectType : BusinessObjectType :
class BusinessObjectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('object',TextType::class)
->add('complement')
->add('status')
->add('durationExpected')
->add('durationAchieved')
->add('client')
->add('projectManager')
->add('business',HiddenType::class,array(
'data' => $options['business_ID']
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => BusinessObject::class,
'business_ID'=>Business::class
]);
}
}
With this code, I get an error Expected argument of type "App\\Entity\\Business or null", "string" given.
通过此代码,我得到了
Expected argument of type "App\\Entity\\Business or null", "string" given.
的错误的Expected argument of type "App\\Entity\\Business or null", "string" given.
I think this have something to do with the function configureOptions() in my Form 我认为这与我的Form中的configureOptions()函数有关
The approach can be: 该方法可以是:
public function new(Request $request,Business $business): Response
{
$businessObject = new BusinessObject();
$form = $this->createForm(BusinessObjectType::class, $businessObject);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// I suppose your setter is `setBusiness`, otherwise use more suitable one
$businessObject->setBusiness($business);
$em = $this->getDoctrine()->getManager();
$em->persist($businessObject);
$em->flush();
Form builder is: 表单生成器为:
builder
->add('object',TextType::class)
->add('complement')
->add('status')
->add('durationExpected')
->add('durationAchieved')
->add('client')
->add('projectManager'); // No business field
Another option is to embed BusinessType
form into BusinessObjectType
, you can read more about form embedding here . 另一种选择是将
BusinessType
表单嵌入到BusinessObjectType
,您可以在此处阅读有关表单嵌入的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.