我正在開發一個Symfony 2.6.1應用程序,並且我使用FormTypes和一個使用註釋作爲驗證的實體來呈現一個表單。表單被提交給FOSRestController的AJAX POST調用。事情是isValid()
函數返回FALSE和我沒有得到任何錯誤信息......symfony2的FOSRestController無法驗證ajax發佈請求
我FOSRestController如下所示:
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;
}
下面
我的相冊實體類:
<?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 ,封裝表格:
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;
}
編輯:
如果我手動使用驗證,它的工作原理:
$formGallery = new Gallery();
$formGallery->setName($this->getRequest()->get('name', NULL));
$validator = $this->get('validator');
$errors = $validator->validate($formGallery);
所以它就像我的GalleryType不使用驗證器。
檢查了你的日誌嗎?如果你的表單完全沒有使用驗證器,你將會得到一個錯誤,因爲'$ name'不能是'null'。你沒有得到'有效!'? –
你可以嘗試用這個代替你在你的控制器中創建表單的行:'$ this-> container-> get('form.factory') - > createNamed('',new \ Law \ AdminBundle \ Form \類型\ GalleryType(),$ gallery);' –
我的答案解決了你的問題嗎? –