dravpuso44681 2018-12-11 23:15
浏览 105
已采纳

如何从Symfony3中提交的表单中获取数据?

I faced up with some non-ordinary situation for me.

1) I have a dependent list that rendering by Symfony FormType like this: enter image description here

2) Location and Instruction fields are depend from Company field.

3) When I change Company field (onchange event js) then goes ajax request that retrieves data from the database and build a dropdown list.

4) But when form is submitted I have an error: enter image description here

Please help me to resolve this. Thanks in advance.

My formType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('hours')
        ->add('messageText', null, ['required' => false])
        ->add('company', null, [
            'choice_label' => 'name',
            'placeholder' => 'Please select company',
            'required' => true
        ])
        ->add('procedure', TextType::class, ['attr' => ['placeholder' => 'Please type code or description'] ])
        ->add('instruction', ChoiceType::class, ['mapped' => false])
        ->add('location', ChoiceType::class, ['mapped' => false])

    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => InstructionNotification::class
    ));
}

Action from controller:

/**
 * @Route("/admin/api/instructions", name="admin_api_instructions")
 * @param Request $request
 * @return JsonResponse
 */
public function getInstructionsByCompanyId(Request $request)
{
    $id = $request->get('id');
    if (!$id) {
        return new JsonResponse('No data', 404);
    }

    $instructions = $this->getDoctrine()->getRepository('OctaneBundle:Instruction')->findInstructionsByCompanyId($id);

    return new JsonResponse($instructions);
}

findInstructionsByCompanyId($id):

public function findInstructionsByCompanyId($id)
{
    $qb = $this->createQueryBuilder('i');
    if ($id) {
        $qb
            ->where('i.company = :id')
            ->setParameter('id', $id);
    }

    return $qb->getQuery()->getResult();
}

response from api (i.e.: admin/api/instructions?id=1):

[{"id":2,"label":"First instruction"},{"id":3,"label":"First instruction"}]

If you need any additional information please leave comments below. Thanks

  • 写回答

2条回答 默认 最新

  • dtp19819 2018-12-13 20:16
    关注

    Thanks for the answer but I found out more elegant solution for my case. So,

    my formType now:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ...
            ->add('instruction', FormattedSelectType::class, ['class' => Instruction::class])
            ->add('location', FormattedSelectType::class, ['class' => Location::class])
        ;
    }
    

    FormattedSelectType:

    class FormattedSelectType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'choice_label' => function (FormattedLabelInterface $entity) {
                    return $entity->getFormattedLabel();
                }
            ));
        }
    
        /**
         * {@inheritdoc}
         */
        public function getParent()
        {
            return EntityType::class;
        }
    }
    

    Etities Location and Instruction entities implement JsonSerializable and custom FormattedLabelInterface interface and have the next methods:

    /**
     * @return string
     */
    public function getFormattedLabel()
    {
        return sprintf(self::LABEL_FORMATTED, $this->zip, $this->city, $this->name, $this->address, $this->phone);
    }
    
    /**
     * @return array|mixed
     */
    public function jsonSerialize()
    {
        return [
            'id' => $this->id,
            'label' => $this->getFormattedLabel()
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)