Symfony2 表单 - 数据从未绑定到它

发布于 2024-11-16 05:11:59 字数 1738 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜清冷一曲。 2024-11-23 05:11:59

问题出在我的帖子数据上。它应该是:

array ( [user] => Array ( [email] => [email protected] ) )

the problem was with my post data. it should have been:

array ( [user] => Array ( [email] => [email protected] ) )
若沐 2024-11-23 05:11:59

您应该尝试在 UserType 表单中添加此方法:

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'User',
    );
}

You should try to add this method in your UserType form:

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'User',
    );
}
伤感在游骋 2024-11-23 05:11:59

您是否使用表单生成器来创建表单?表单构建期望名称遵循一定的格式,而不仅仅是具有 email 字段。您通常会创建表单,然后检查请求是否是 Post,如果不是则在模板中呈现表单。

$form = $this->createForm(new UserType(), $user);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
     $form->bindRequest($request);
     ...
     return...
}
return array('form' => $form->createView());

然后在模板中使用 {{ 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.

$form = $this->createForm(new UserType(), $user);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
     $form->bindRequest($request);
     ...
     return...
}
return array('form' => $form->createView());

Then in your template you use {{ form_widget(form) }} or the related functions that allow you to render parts of the form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文