私はSymfony 2.6.1アプリケーションを開発しています。私はFormTypesとエンティティを使用してフォームをレンダリングしています。フォームはFOSRestControllerにAJAX POSTコールを送信します。symfony2のFOSRestControllerがajax投稿要求を検証できません
class RestGalleryController extends FOSRestController{
/**
* @Route(requirements={"_format"="json"})
*/
public function postGalleriesAction(\Symfony\Component\HttpFoundation\Request $request){
return $this->processForm(new \Law\AdminBundle\Entity\Gallery());
}
private function processForm(\Law\AdminBundle\Entity\Gallery $gallery){
$response = array('result' => 'Default');
$gallery->setName('TEST'); //Just added this to be sure it was a problem with the validator
$form = $this->createForm(
new \Law\AdminBundle\Form\Type\GalleryType(),
$gallery
);
$form->handleRequest($this->getRequest());
if ($form->isValid()) {
$response['result'] = 'Is Valid!';
}else{
var_dump($form->getErrorsAsString());
die;
}
return $response;
}
以下
マイギャラリーEntityクラス:
<?php
namespace Law\AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Gallery
*
* @ORM\Table(name="gallery")
* @ORM\Entity
*/
class Gallery{
/**
* @var string
* @Assert\NotBlank()
* @ORM\Column(name="name", type="text", nullable=false)
*/
private $name;
public function __construct(){
$this->images = new ArrayCollection();
}
/**
* Set name
*
* @param string $name
* @return Gallery
*/
public function setName($name){
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName(){
return $this->name;
}
}
GalleryType次のようなものがisValid()
関数がFALSEを返していると私はエラーメッセージを取得していない...
私FOSRestControllerに見えます、フォームをカプセル化する:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class GalleryType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('name');
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Law\AdminBundle\Entity\Gallery',
'csrf_protection' => false,
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Gallery';
}
}
最後に、私のap P /設定/ config.yml次のように、検証が設定されています
validation: { enable_annotations: true }
私も失敗し、以下の機能を試してみた検証エラーを取得するには:
private function getErrorMessages(\Symfony\Component\Form\Form $form) {
$errors = array();
foreach ($form->getErrors() as $key => $error) {
if ($form->isRoot()) {
$errors['#'][] = $error->getMessage();
} else {
$errors[] = $error->getMessage();
}
}
foreach ($form->all() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getErrorMessages($child);
}
}
return $errors;
}
EDIT:
私は手動でバリデータを使用している場合は、それが動作:
$formGallery = new Gallery();
$formGallery->setName($this->getRequest()->get('name', NULL));
$validator = $this->get('validator');
$errors = $validator->validate($formGallery);
なので、GalleryTypeがバリデーターを使用していなかったようです。
ログを確認しましたか?フォームがバリデーターをまったく使用しなかった場合、 '$ name'は' null'にはならないので、エラーが発生します。あなたは 'Is Is Valid!'を取得していませんでしたか? –
コントローラのフォームを作成する行を次のように置き換えることができます: '$ this-> container-> get( 'form.factory') - > createNamed( ''、new \ Law \ AdminBundle \ Form \ \ GalleryType()、$ gallery); –
私の答えはあなたの問題を解決しましたか? –