我正在尝试在Symfony 3项目中创建用户个人资料编辑表单。
在这次尝试中,我编写了以下代码:
控制器:
/**
* @Route("/profile/edit", name="user_edit_profile", methods={"GET"})
* @Security("has_role('ROLE_USER')")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editMyProfileAction($form = null) {
return $this->render('user/edit_profile.html.twig', [
'form' => ($form !== null) ? $form->createView() : $this->createForm(UserProfileType::class, $this->getUser())->createView()
]);
}
/**
* @Route("/profile/edit", name="user_update_profile", methods={"POST"})
* @Security("has_role('ROLE_USER')")
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function updateMyProfileAction(Request $request) {
$user = $this->getUser();
$form = $this->createForm(UserProfileType::class, $user);
$form->handleRequest($request);
if(!$form->isValid()) {
return $this->forward('AppBundle:User:editMyProfile', [
'form' => $form
]);
}
$this->get('medievistes.user_manager')->updateUser($user);
return $this->redirectToRoute('user_profile');
}
UserType,用于注册:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\{AbstractType, FormBuilderInterface};
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\{EmailType, TextType, RepeatedType, PasswordType};
class UserType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class)
->add('email', EmailType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'users.registration.password_mismatch',
'first_options' => ['label' => 'Password'],
'second_options' => ['label' => 'Repeat Password'],
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User'
));
}
}
然后是UserProfileType,用于用户版:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\{AbstractType, FormBuilderInterface};
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\{
EmailType,
TextType,
RepeatedType,
PasswordType,
DateType
};
class UserProfileType extends UserType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('username', TextType::class, [
'disabled' => true
])
->add('email', TextType::class, [
'disabled' => true
])
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('birthdate', DateType::class, [
'placeholder' => '',
'widget' => 'single_text',
'required' => false,
'inherit_data' => true,
'empty_data' => null
])
->add('avatar', AvatarType::class)
->remove('plainPassword')
;
}
}
My Doctrine字段$ birthdate可以为空,我还没有设置任何验证限制。如你所见,我为“birthdate”字段尝试了很多选项:
`在第13行的form_div_layout.html.twig中,在呈现模板期间抛出异常(“Catchable Fatal Error:类AppBundle \ Entity \ User的对象无法转换为字符串”)。 / p>
我删除了__toString方法,重新获得了我的开发者荣誉,在此之前观察返回的用户名被用作生日输入(真实WTF)的值属性。我真的会相信这是一个框架问题。但我希望我错了。我不经常使用表单组件。
请拯救我的灵魂。