Symfony动态添加表单输入并转换为JSON [英] Symfony dynamically add form input and convert to JSON

查看:104
本文介绍了Symfony动态添加表单输入并转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这很难解释. 事情是,我有这个实体

So this is a bit hard to explain. thing is, I have this entity

class TypeParking
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=55)
     */
    private $libelle;

    /**
     * @ORM\Column(type="time", nullable=true)
     */
    private $tempsmax;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $jourdebut;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $jourfin;

    /**
     * @ORM\Column(type="json_array", nullable=true)
     */
    private $heurstravail;

    /**
     * @ORM\Column(type="json_array", nullable=true)
     */
    private $exception;

这是我的控制器:


    /**
     * @Route("/new", name="type_parking_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $typeParking = new TypeParking();
        $form = $this->createForm(TypeParkingType::class, $typeParking);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($typeParking);
            $entityManager->flush();

            return $this->redirectToRoute('type_parking_index');
        }

        return $this->render('type_parking/new.html.twig', [
            'type_parking' => $typeParking,
            'form' => $form->createView(),
        ]);
    }


<?php

namespace App\Form;

use App\Entity\TypeParking;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TypeParkingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('libelle')
            ->add('tempsmax')
            ->add('jourdebut')
            ->add('jourfin')
            ->add('heurstravail')
            ->add('exception')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => TypeParking::class,
        ]);
    }
}

看到那个异常字段?它的类型是数据库中的JSON. 它必须包含名称,开始日期,结束日期以及开始时间和结束时间. 像这样 https://imgur.com/a/2qrz5yy 每当我按下加号按钮时,我都可以添加另一个异常字段(JQuery). 当我提交表单时,整个异常字段将解析为JSON,并与表单的其余部分一起保存到数据库中. 我的数据库: https://imgur.com/a/UonYT3W

See that exception field ? It's type is JSON in the database. it must contain a name, a starting date, and ending date and starting time and ending time. like this https://imgur.com/a/2qrz5yy whenever I press that Plus button I can add another exception field (JQuery). and when I submit the form this whole exception field gets parsed into a JSON and saved into the databse alongside the rest of the form. My database: https://imgur.com/a/UonYT3W

我已经尝试使它工作好几天了,但我却无能为力.

I've been trying to get this to work for days now and I couldn't do anything.

推荐答案

您的表单类型非常简单.明确为您的例外字段添加(子)字段.

your form type is very very minimalistic. explicitly add (sub)fields for your exception field.

<?php

namespace App\Form;

use App\Entity\TypeParking;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
// don't forget to add the types here!
use Symfony\Component\Form\Extension\Core\Type\TextType;

class TypeParkingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('libelle')
            ->add('tempsmax')
            ->add('jourdebut')
            ->add('jourfin')
            ->add('heurstravail')
            ->add('exception_name', TextType::class, ['property_path' => 'exception[name]')
            // add other fields of exception, look at 
            // https://symfony.com/doc/current/reference/forms/types.html
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => TypeParking::class,
        ]);
    }
}

我希望这对您有帮助...

I hope this helps ...

但是,表单组件(属性访问器)将尝试获取异常,因此我们必须通过将以下内容添加到TypeParking实体类中来提供帮助:

however, the form component (property accessor) will try to get the exception, so we have to help by adding the following to the TypeParking entity class:

public function getException() {
    return array_merge([
        'name' => '', 
        // other sub-fields "empty" values
    ], $this->exception ?? [] // prevent array_merge from failing if exception is empty
    );
}

这篇关于Symfony动态添加表单输入并转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆