我正在尝试扩展一个包的形式,我收到了这个错误:
无法加载类型 “App \ Form \ Extension \ UserRegistrationFormExtension”:类没有 实现“Symfony \ Component \ Form \ FormTypeInterface”。
SRC /形式/扩展/ UserRegistrationFormExtension.php
namespace App\Form\Extension;
use App\Entity\User;
use MyBundle\AuthenticationBundle\Form\UserRegistrationForm;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
class UserRegistrationFormExtension extends AbstractTypeExtension
{
/**
* Returns the name of the type being extended.
*
* @return string The name of the type being extended
*/
public function getExtendedType()
{
return UserRegistrationForm::class;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//code
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'validation_groups' => ['Default', 'Registration'],
'allow_extra_fields' => true,
'cascade_validation' => true,
]);
}
}
配置/ services.yaml
App\Form\Extension\UserRegistrationFormExtension:
tags:
- { name: form.type_extension, extended_type: MyBundle\AuthenticationBundle\Form\UserRegistrationForm }
这是堆栈跟踪:
Symfony\Component\Form\Exception\InvalidArgumentException:
Could not load type "App\Form\Extension\UserRegistrationFormExtension": class does not implement "Symfony\Component\Form\FormTypeInterface".
at vendor/symfony/form/FormRegistry.php:89
at Symfony\Component\Form\FormRegistry->getType('App\\Form\\Extension\\UserRegistrationFormExtension')
(vendor/symfony/form/FormFactory.php:58)
at Symfony\Component\Form\FormFactory->createBuilder('App\\Form\\Extension\\UserRegistrationFormExtension', object(User), array())
(vendor/symfony/form/FormFactory.php:30)
at Symfony\Component\Form\FormFactory->create('App\\Form\\Extension\\UserRegistrationFormExtension', object(User), array())
(vendor/symfony/framework-bundle/Controller/ControllerTrait.php:311)
at Symfony\Bundle\FrameworkBundle\Controller\Controller->createForm('App\\Form\\Extension\\UserRegistrationFormExtension', object(User))
(vendor/mybundle/auth/Controller/LoginStepsController.php:149)
at MyBundle\AuthenticationBundle\Controller\LoginStepsController->register(object(Request), object(GuardAuthenticatorHandler), object(LoginFormAuthenticator), 'user@user.com')
(vendor/symfony/http-kernel/HttpKernel.php:149)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:66)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:188)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:37)
我已经按照这个文档(https://symfony.com/doc/current/form/create_form_type_extension.html)了,但显然我做错了什么,我无法弄清楚是什么。