我有用户,可以通过表格注册:姓名,姓氏,电子邮件,电话,密码,用户名。
注册用户可以更改:姓名,姓名,电话
我的用户类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('lastname', 'text')
->add('email', 'email')
->add('phone', 'text')
->add('password', 'text')
->add('username', 'text');
}
我应该如何呈现不同的注册和更新表单?
答案 0 :(得分:1)
唯一的区别是您将第二个属性传递给createForm
方法。如果您传递空实体,它将是添加新用户的表单。如果您传递具有用户实体的填充数据的实体,则它将是一个编辑表单。所以:
$yourNewUserForm = $this->createForm(new UserType(), new UserEntity());
$userEntity = $this->getDoctrine()->getRepository('YourBundle:User')->find(1);
$yourEditUserForm = $this->createForm(new UserType(), $userEntity);
注意:以上代码假设您正在课堂上展开Controller
并且您可以使用createForm
和getDoctrine()
方法,但请考虑这是否应该是其中的一部分控制器或它应该移动到其他地方(这可能是一个更好的主意)