私はSymfony2でシンプルなRESTAPIを作成するために、このガイドにできるだけ近づいています。
残念ながら、私が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: コンテンツtof $ _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>