Symfony2 表单 - 数据从未绑定到它
我在 Symfony2(beta 5)中有一个简单的表单,但发布数据从未绑定到该表单。以下是我的课程(为简洁而进行了修剪):
/**
* Represents a User
*
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", unique="true", length="150")
* @Assert\Email()
*/
protected $email;
/**
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return string $email
*/
public function getEmail()
{
return $this->email;
}
}
表单生成器:
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('email');
}
}
操作:
public function addAction()
{
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->bindRequest($request);
print_r($_POST); // fine - contains an email address
echo 'email: ';
print_r($user->getEmail()); // always empty
if ($form->isValid()) // never valid
{
// ....
发布数据:
array([email] => '[email protected]')
我的设置有什么问题?这是我用不同模型制作的第二种形式,所以我显然做错了。
我是否可能将“电子邮件”作为密钥发布,而不是像“user_email”这样更复杂的内容?我还没有渲染表单 - 我只是手动提交帖子数据,因为这是针对网络服务的。
谢谢
I have a simple form in Symfony2 (beta 5), but the post data is never bound to the form. Here are my classes (trimmed for brevity):
/**
* Represents a User
*
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", unique="true", length="150")
* @Assert\Email()
*/
protected $email;
/**
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return string $email
*/
public function getEmail()
{
return $this->email;
}
}
The form builder:
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('email');
}
}
The action:
public function addAction()
{
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->bindRequest($request);
print_r($_POST); // fine - contains an email address
echo 'email: ';
print_r($user->getEmail()); // always empty
if ($form->isValid()) // never valid
{
// ....
Post data:
array([email] => '[email protected]')
What's wrong with my set-up? This is the second form I've made with a different model, so I'm obviously doing something wrong.
Is it perhaps that I'm posting 'email' as the key instead of something more elaborate like 'user_email'? I haven't rendered the form - I'm just submitting post data by hand because this is for a web service.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在我的帖子数据上。它应该是:
the problem was with my post data. it should have been:
您应该尝试在 UserType 表单中添加此方法:
You should try to add this method in your UserType form:
您是否使用表单生成器来创建表单?表单构建期望名称遵循一定的格式,而不仅仅是具有
email
字段。您通常会创建表单,然后检查请求是否是 Post,如果不是则在模板中呈现表单。然后在模板中使用
{{ form_widget(form) }}
或允许您呈现表单部分的相关函数。Are you using the form builder to create the form? The form build expects the name to follow a certain format, not just have an
email
field. You would normally create the form, then check if the request is a Post, if not render the form in the template.Then in your template you use
{{ form_widget(form) }}
or the related functions that allow you to render parts of the form.