Symfony 4:形式即服务,注入服务:参数太少

时间:2018-10-08 13:25:18

标签: php symfony4

我会按照官方指示尝试以表格形式提供服务: https://symfony.com/doc/current/form/form_dependencies.html

这是我的services.yaml:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false      
class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

App\Service\:
    resource: '../src/Service/*'

App\Form\:
    resource: '../src/Form/*'

我的表单如下:

<?php
namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

use Doctrine\Common\Persistence\ObjectManager;

use App\Entity\Contrat;
use App\Entity\Society;
use App\Entity\Client;
use App\Form\DataTransformer\ClientToNumberTransformer;
use App\Form\DataTransformer\SocietyToNumberTransformer;



class ContratType extends AbstractType{

    private $manager;

    public function __construct(SocietyToNumberTransformer $manager){ // this is the breakpoint from error
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('...')
            ...
        ;
        // Old fashion way for testing only ...
        $builder->get('client')->addModelTransformer(new ClientToNumberTransformer($this->manager));
        $builder->get('society')->addModelTransformer(new SocietyToNumberTransformer($this->manager));
    }

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

它看起来很漂亮……当我与邮递员测试时,出现此错误:

  

函数App \ Form \ ContratType :: __ construct(),0的参数太少   在第92行的/code/vendor/symfony/form/FormRegistry.php中传递,并且   正好是1个

我不明白,我在自动装配调试命令中发现了我的fomr:

php bin/console debug:autowiring
  

App \ Form \ ContratType
  App \ Form \ DataTransformer \ ClientToNumberTransformer
  App \ Form \ DataTransformer \ SocietyToNumberTransformer

和控制器:

  public function addContrat(Request $request, FormData $formData){

        $em = $this->getDoctrine()->getManager('default');

        $data = $formData->getRequestData($request);
        var_dump($data);

        $contrat = new Contrat;
        $form = $this->factory->createBuilder(ContratType::class, $contrat)->getForm();

        $form->submit($data);

        if($form->isValid()){
            $em->persist($contrat);
            $em->flush();

            return $this->response(
                $this->serialize(
                    $contrat, 
                    ['group1']
                )
            );
        }
        return $this->response('error', 500);
    }

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试替换

$form = $this->factory->createBuilder(ContratType::class, $contrat)->getForm();

$form = $this->createForm(ContratType::class, $contrat);

https://symfony.com/doc/current/forms.html#creating-form-classes

我遇到了同样的问题,但是当我从服务内部构建表单时,我仍然卡住了;-)

希望有帮助