2

我一直在尽可能地遵循本指南,以便在 Symfony2 中创建一个简单的 REST API。

不幸的是,无论我向 API 发布什么,我总是得到:

{"children":{"firstName":{"errors":["This value should not be blank."]},"lastName":{"errors":["This value should not be blank."]},"email":{"errors":["This value should not be blank."]},"password":{"errors":["This value should not be blank."]},"dob":{"errors":["This value should not be blank."],"children":{"year":[],"month":[],"day":[]}},"tutorialWatched":{"errors":["This value should not be blank."]},"challengeEmails":{"errors":["This value should not be blank."]},"mailingList":{"errors":["This value should not be blank."]}}}

我的验证如下:

LifeMirror\APIBundle\Model\Users:
    properties:
        firstName:
            - NotBlank:
        lastName:
            - NotBlank:
        email:
            - NotBlank:
            - Email:
        password:
            - NotBlank:
        dob:
            - NotBlank:
            - Date:
        tutorialWatched:
            - NotBlank:
            - Choice:
                choices: [0, 1]
        challengeEmails:
            - NotBlank:
            - Choice:
                choices: [0, 1]
        mailingList:
            - NotBlank:
            - Choice:
                choices: [0, 1]

我的控制器是:

class RegisterController extends Controller
{
    public function indexAction()
    {
        return $this->processForm(new Users());
    }

    private function processForm(Users $user)
    {
        $statusCode = $user->isNew() ? 201 : 204;

        $form = $this->createForm(new UsersType(), $user);
        $form->bind($this->getRequest());

        if ($form->isValid()) {
            $user->save();

            $response = new Response();
            $response->setStatusCode($statusCode);
            return $response;
        }
        $view = View::create($form, 400);
        $view->setFormat('json');
        return $view;
    }
}

我可以 var_dump$this->getRequest()并且可以看到数据在那里,但我不确定验证器为什么抱怨。

编辑:

这是表格:

namespace LifeMirror\APIBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class UsersType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName');
        $builder->add('lastName');
        $builder->add('email');
        $builder->add('password');
        $builder->add('dob');
        $builder->add('tutorialWatched');
        $builder->add('challengeEmails');
        $builder->add('mailingList');
    }

    /**
     * {@inheritdoc}
     */
    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'LifeMirror\APIBundle\Model\Users',
            'csrf_protection'   => false,
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'users';
    }
}

我的意见: 在此处输入图像描述

编辑 2: $_REQUEST 的内容:

array(9) { ["firstName"]=> string(5) "James" ["lastName"]=> string(6) "Hadley" ["email"]=> string(9) "fd@fd.com" ["password"]=> string(7) "test123" ["dob"]=> string(33) "{'year':1991,'month':08,'day':02}" ["location"]=> string(9) "Lancaster" ["tutorialWatched"]=> string(1) "0" ["challengeEmails"]=> string(1) "0" ["mailingList"]=> string(1) "0" }

编辑 3: HTML 表单:

<form action="http://www.lifemirror.org/api/register/" method="post">
    <input type="hidden" name="firstName" value="James" />
    <input type="hidden" name="lastName" value="Hadley" />
    <input type="hidden" name="email" value="fd@fd.com" />
    <input type="hidden" name="password" value="test123" />
    <input type="hidden" name="dob" value="{'year':1991,'month':08,'day':02}" />
    <input type="hidden" name="location" value="Lancaster" />
    <input type="hidden" name="tutorialWatched" value="0" />
    <input type="hidden" name="challengeEmails" value="0" />
    <input type="hidden" name="mailingList" value="0" />
    <input type="submit" />
</form>
4

4 回答 4

1

如果您使用 FOSRestBundle 和主体侦听器,则请求不再接受任何 application/x-www-form-urlencoded 内容。您不能使用 HTML 表单发送数据。

REST Web 服务意味着,您将请求数据作为 JSON 或 XML 发送,就像您收到响应一样。

对于测试,您可以将NelmioApiDocBundle添加到您的项目中,它们有一个出色的测试沙箱。

于 2013-04-29T11:51:22.350 回答
1

问题是您没有在操作中考虑表单名称。顺便说一句,您的表单名称是“用户”。

将您的操作更改为

class RegisterController extends Controller
{
    public function indexAction()
    {
        return $this->processForm(new Users());
    }

    private function processForm(Users $user)
    {
        $statusCode = $user->isNew() ? 201 : 204;

        $form = $this->createForm(new UsersType(), $user);
        $form->bind(array('users'=>$this->getRequest()->query->all()));

        if ($form->isValid()) {
            $user->save();

            $response = new Response();
            $response->setStatusCode($statusCode);
            return $response;
        }
        $view = View::create($form, 400);
        $view->setFormat('json');
        return $view;
    }
}

现在它应该可以正常工作了。

新的绑定是让它工作的关键

$form->bind(array('users'=>$this->getRequest()->query->all()));

PS:请注意,如果您使用 Twig 来呈现该表单,则无需更改代码,因为表单名称将与 HTML 标记一起呈现。

您的 HTML 表单应该类似于

<form>
    <input name="users[firstName]">
    <input name="users[lastName]">
    <input name="users[email]">
    <input name="users[password]">
    <input name="users[tutorialWatched]">
    <input name="users[challengeEmails]">
    <input name="users[mailingList]">
</form>    
于 2013-01-27T21:54:30.863 回答
0

我也遇到了这个问题。我能够通过绑定(POST)请求变量来使其工作,如下所示:

$form->bind($request->request->all());

尽管如此,我还是觉得默认方法不起作用很奇怪......

编辑:好的,我想通了。这是一个命名空间的事情。如果我使用这样的名称空间发布变量,myformname[fieldname]而不是只是fieldname,那么它适用于

$form->bind($request);
于 2013-04-29T11:44:49.907 回答
0

奇怪的是,在我的例子中,每个字段都必须在控制器中手动指定:

    $form->bind(array(
        "firstName" => $this->getRequest()->request->get('firstName'),
        "lastName" => $this->getRequest()->request->get('lastName'),

等等

于 2013-01-28T20:44:54.773 回答