我一直在尽可能地遵循本指南,以便在 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>