我是Symfony的新手,通过创建一个小项目来学习Symfony2。
/** 用户类型构建表单函数 */
$age_choices = array('18'=>'18+','25'=>'25+','30'=>'30+','40'=>'40+','50'=>'50+');
$complextion_choices = array('Moderate'=>'Moderate','Fair'=>'Fair');
$builder
->add('first_name', 'text')
->add('last_name', 'text')
->add('email', 'text')
->add('phone', 'text', array('required' => false))
->add('age', 'choice', array('choices' => $age_choices) )
->add('complextion', 'choice', array('choices' => $complextion_choices) );
/** 创建用户表单 ****/**
$user = new Users();
$form = $this->createForm(new UsersType());
return $this->render('TrytillUserBundle:User:signup.html.twig', array('form' =>
$form->createView()));
现在,当我提交此表格时,请使用以下方式获取它。
$user = new Users();
$form = $this->createForm(new UsersType(), $user);
$form->bindRequest($request);
if ($form->isValid()) {
/// some task here.
}
return $this->render('TrytillUserBundle:User:signup.html.twig', array('form' => $form->createView()));
问题是在绑定请求行上它告诉我实体用户中不存在属性first_name。
我使用原则创建了用户实体,它是实体/用户中的私有$firstName.php
感谢您的任何帮助。
尝试->add('firstName', 'text')
而不是在UserType::buildForm
函数中->add('first_name', 'text')
,last_name
也是如此。