Symfony FromEvents 1参数必须是。的实例

时间:2018-04-12 13:47:31

标签: php symfony

这里的错误并不明确,我认为它与参数之间的类型差异有关但我还没有找到解决这个问题的方法...... EventListener正在接收2个参数,但是它没有提供哪些信息是带有问题的“1”参数,但我认为它是第2个,因为错误说 UserBundle的一个实例\ Form上的Form \ FormEvent ,第一个参数是来自FormEvents类的泛型事件,该事件是复数形式。

错误显示:

  

类型错误:参数1传递给   UserBundle \ Form \ UserType :: UserBundle \ Form {closure}()必须是   UserBundle \ Form \ FormEvent的实例,实例   给出了Symfony \ Component \ Form \ FormEvent

<?php

namespace UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvents;
class UserType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('username',HiddenType::class)
        ->add('firstname',TextType::class)
        ->add('lastname',TextType::class)
        ->add('email', RepeatedType::class, 
            array('type' => EmailType::class, 'invalid_message' => 'The email fields must match.','options' 
                => array('attr' => array('class' => 'email-field')),'required' => true, 'first_options'  
                => array('label' => 'Email'),'second_options' => array('label' => 'Confirm email'),))
        ->add('password', RepeatedType::class, 
            array('type' => PasswordType::class, 'invalid_message' => 'The password fields must match.','options' 
                => array('attr' => array('class' => 'password-field')),'required' => true, 'first_options'  
                => array('label' => 'Password'),'second_options' => array('label' => 'Confirm password'),))
        ->add('save',SubmitType::class)
        ->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
            $data = $event->getData();
            $form = $event->getForm();
            if ($data['firstname'] !== null && $data['lastname'] !== null){
                $username = $data['firstname']." ".$data['lastname'];
                $data['username'] = $username;
                $event->setData($data); 
            }
        });
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'UserBundle\Entity\User'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'user';
    }


}

我已按照symfony文档中给出的步骤进行了

1 个答案:

答案 0 :(得分:4)

您错过了use Symfony\Component\Form\FormEvent; 类导入。

UserBundle\Form\FormEvent

由于您没有导入它,因此它使用了当前名称空间(Open the Alfred3 Go to Workflows Click + sign at the bottom left sidebar Choose Blak Workflow Name: Vim Launcher Description: Opens a file in the terminal inside vim editor 1. Right-click > Trigger > press cmd-alt-v 2. Right-click > Inputs > Keyword > open in vim and title also open in vim 3. Right-click > Actions > Run NSAppleScipt )。但是这个课程不存在。