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条)

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用