以编辑形式管理外地人口

时间:2019-04-07 10:38:19

标签: php symfony symfony4

我正在使用symfony 4,并且具有以下形式:


<?php

namespace App\Form;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

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('Exception_Name', TextType::class, ['property_path' => 'exception[name]'])
            ->add('Starting_date', DateType::class, [
                'property_path' => 'exception[datedebut]',
            ])
            ->add('Ending_date', DateType::class, [
                'property_path' => 'exception[datefin]',
            ])
            ->add('Starting_time', TimeType::class, ['property_path' => 'exception[heuredebut]'])
            ->add('Ending_time', TimeType::class, ['property_path' => 'exception[heurefin]'])


        ;
    }

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

当我创建一个新表单时,我通常会手动获取Exception_Name,Starting_date,Ending_date,Starting_time和Ending_time的值,并将它们放入单个数据库字段中的单个json数组中。 但是,当我去编辑表单时,json内的数据不会被拆分以填充每个字段。

让我们说我有这个json数组:{“ Exceptions”:{“ name”:“ 6fdfs”,“ StartDate”:“ 2015-03-03”,“ StartHour”:“ 00:00:00”, “ EndingDate”:“ 2015-03-03”,“ EndingHour”:“ 00:00:00”}} 我要取一个名称值,并用它来填充Exception_Name字段,等等...

TL; DR:如何控制我要如何预先填充编辑表单的每个字段

编辑: 这是我的数据映射器

<?php
// src/Form/DataMapper/ColorMapper.php
namespace App\Form\DataMapper;

use App\Painting\Color;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormInterface;

final class TypeParkingMapper implements DataMapperInterface
{
    /**
     * @param TypeParking|null $data
     */
    public function mapDataToForms($data, $forms)
    {
        // there is no data yet, so nothing to prepopulate
        if (null === $data) {
            return;
        }

        /** @var FormInterface[] $forms */
        $forms = iterator_to_array($forms);
       // initialize form field values
       $Excep=$data->getException();
       $forms['Exception_Name']->setData($Excep['Exceptions']['name']);
       $forms['Starting_date']->setData($Excep['Exceptions']['StartDate']);
       $forms['Ending_date']->setData($Excep['Exceptions']['EndingDate']);
       $forms['Starting_time']->setData($Excep['Exceptions']['StartHour']);
       $forms['Ending_time']->setData($Excep['Exceptions']['EndingHoure']);



    }

    public function mapFormsToData($forms, &$data)
    {
        /** @var FormInterface[] $forms */
        $forms = iterator_to_array($forms);

        // as data is passed by reference, overriding it will change it in
        // the form object as well
        // beware of type inconsistency, see caution below
        $data = new TypeParking(
            $forms['Exception_Name']->getData()

        );
    }
}

因此,当我编辑对象时(因为getException()从我的数据库返回json数组),它可以很好地工作,但是当我创建一个新对象时,由于没有数据要获取,它会引发错误。 那么,有什么方法可以在我创建新表单时关闭映射器,而仅在编辑表单时将其激活吗?

错误是:“注意:未定义的索引:异常”

1 个答案:

答案 0 :(得分:1)

您可以编写DataMapper来完全控制如何将数据映射到表单。