对于我使用Symfony 3.4(使用Symfony Flex)的应用程序,我想要一个注册系统。 我的系统非常简单,我有一个包含3个字段的表单。
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class)
->add('email', TextType::class)
->add('password', PasswordType::class)
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class,
'csrf_protection' => true,
'csrf_field_name' => '_token'
));
}
}
使用我的控制器
public function register(Request $request): Response
{
$user = new User();
$form = $this->createForm(RegisterType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$this->addFlash('success', 'Bravo');
return $this->redirectToRoute('default_home');
}
return $this->render('user/register.html.twig', [
'form' => $form->createView()
]);
}
我控制器第32行的错误Expected argument of type "App\Entity\void", "string" given
所以$form->handleRequest($request);
感谢您的帮助!