empty_data不适用于复合形式,或实体未被实例化(ArguementCountError:函数参数太少)

问题描述 投票:3回答:1

我有一个有很多Companys的Employee。在我的表单中,我希望用户能够动态添加员工(足够简单)。 EmployeeType(一个AbstractType)是复合的,包含名字和姓氏。在表单提交时,Symfony似乎不会将表单中的数据转移到“新”员工的构造函数中。我得到ArgumentCountError: Too few arguments to function Employee::__construct() ... 0 passed in ... and exactly 3 expected。显示和编辑现有员工的工作,所以我相信我的关系等都是正确的。

缩写代码:

Company

class Company
{
    protected $employees;

    public function __construct()
    {
        $this->employees = new ArrayCollection();
    }

    public function addEmployee(Employee $employee)
    {
        if ($this->employees->contains($employee)) {
            return;
        }
        $this->employees->add($employee);
    }

    public function removeEmployee(Employee $employee)
    {
        if (!$this->employees->contains($employee)) {
            return;
        }
        $this->employees->removeElement($employee);
    }
}

Employee

class Employee
{
    // ... firstName and lastName properties...

    public function __construct(Company $company, $firstName, $lastName)
    {
        $this->company = $company;
        $this->company->addEmployee($this);
    }

    // ...getter and setter for firstName / lastName...
}

CompanyType

class CompanyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('employees', CollectionType::class, [
            'entry_type' => EmployeeType::class,
            'allow_add' => true,
            'allow_delete' => false,
            'required' => false,
        ]);
        // ...other fields, some are CollectionType of TextTypes that work correctly...
    }
}

EmployeeType

class EmployeeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName')
            ->add('lastName');
    }

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

CompanyController

class CompanyController
{
    // Never mind that this is a show and not edit, etc.
    public function showAction()
    {
        // Assume $this->company is a new or existing Company
        $form = $this->createForm(CompanyType::class, $this->company);

        $form->handleRequest($this->request);

        if ($form->isSubmitted() && $form->isValid()) {
            $company = $form->getData();

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($company);
            $entityManager->flush();
        }

        // set flash message, redirect, etc.
    }

    // ...render view...
}

修改现有员工时,上述操作将起作用,而不是在创建新员工时。从Symfony代码中调试,我可以看到新员工没有数据存在,所以它试图在empty_data中找到CompanyType的闭包或定义。我已经尝试了这一切(通过configureOptionsempty_data选项在构建CompanyType::buildForm表格时),例如https://symfony.com/doc/current/form/use_empty_data.html。我的直觉告诉我,我甚至不需要这样做,因为表单数据不应该是空的(我明确地填写了字段)。

我也尝试过使用模型变换器。在这种情况下,从形式(传递给new CallbackTransformer的第二个函数参数)的转换甚至没有被击中。

视图在添加新员工字段时正确设置名称属性,例如form[employees][1][firstName]等。这不是问题。它还将正确的数据发送到控制器。我通过CompanyType::onPreSubmit(使用事件监听器)检查表单提交数据来确认这一点。

我还有CollectionTypeTextType用于CompanyType的其他东西,那些工作正常。所以问题似乎与EmployeeType是复合(包含多个字段)的事实有关。

希望以上内容足以说明问题所在。有任何想法吗?

更新:

似乎问题是没有为Symfony工作的Employee的实例化。在内部,每个字段都传递给Symfony\Component\Form\Form::submit()。对于现有员工,还有一个Employee传入。对于新的,它是null。这就解释了为什么它正在寻找empty_data,但我不知道为什么我不能让empty_data工作。

symfony symfony-forms
1个回答
0
投票

解决方案是定义化合物形式的empty_data,而不是CollectionType形式。

我的情况有点奇怪,因为我还需要在Company中使用EmployeeType的实例,因为它必须传递给Employee的构造函数。我通过将公司作为表格选项传递到configureOptions(由控制器提供),然后进入entry_options来实现这一点。我不知道这是否是最佳做法,但它有效:

CompanyController

确保我们传入公司实例,因此在构建新的EmployeeType时可以在Employee中使用它:

$form = $this->createForm(CompanyType::class, $this->company, [
    // This is a form option, valid because it's in CompanyType::configureOptions()
    'company' => $this->company,
]);

CompanyType

class CompanyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('employees', CollectionType::class, [
            // ...

            // Pass the Company instance to the EmployeeType.
            'entry_options' => [ 'company' => $options['company'] ],

            // This was also needed, apparently.
            'by_reference' => false,
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            // Allows the controller to pass in a Company instance.
            'company' => null,
        ]);
    }
}

EmployeeType

在这里,我们确保empty_data从表单数据中正确构建Employee。

class EmployeeType extends AbstractType
{
    private $company;

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName')
            ->add('lastName');

        // A little weird to set a class property here, but we need the Company
        // instance in the 'empty_data' definition in configureOptions(),
        // at which point we otherwise wouldn't have access to the Company.
        $this->company = $options['company'];
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Employee::class,
            'empty_data' => function (FormInterface $form) use ($resolver) {
                return new Employee(
                    $this->company,
                    $form->get('firstName')->getData(),
                    $form->get('lastName')->getData(),
                );
            },
        ]);
    }
}

中提琴!我现在可以添加新员工了。

希望这有助于其他人!

© www.soinside.com 2019 - 2024. All rights reserved.