所以我有一个表单类型“PictureType”:
<?php
namespace AB\MyBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class PictureType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder ->add('picture','file');
}
public function getName()
{
return 'ab_mybundle_picturetype';
}
public function useDefaultOptions(array $options)
{
return array(
'data_class' => 'AB\MyBundle\Form\Model\Picture'
);
}
}
实体图片:
<?php
namespace AB\MyBundle\Form\Model;
use Symfony\Component\Validator\Constraints as Assert;
class Picture
{
/**
* @Assert\Image(maxSize="800000")
*/
public $picture;
}
控制器:
use AB\MyBundle\Form\Type\PictureType;
use AB\MyBundle\Form\Model\Picture;
...
...
$form = $this->createForm(new PictureType, new Picture);
if( $this->get('request')->getMethod() == 'POST' )
{
echo "1"; // 1 is printed
$form->bindRequest($this->get('request')); // this line gives the error
echo "2"; // 2 is not printed
if( $form->isValid() )
{
}
}
return $this->render("ABMyBundle:Default:pic.html.twig",array(
'form' => $form->createview()
));
当我提交表单时,它给我一个500错误
"The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?"
有人能帮助我:(?
答案 0 :(得分:2)
$form = $this->createForm(new PictureType, new Picture);
if( $this->get('request')->getMethod() == 'POST' )
{
echo "1";
$form->bind($request);
if( $form->isValid() )
{
echo "2";
}
}
参考链接: http://symfony.com/blog/form-goodness-in-symfony-2-1 https://github.com/symfony/symfony/pull/4811
答案 1 :(得分:1)
不要使用echo,控制器必须返回响应:
$form->bindRequest($this->get('request')); // this line gives the error
if( $form->isValid() )
{
return new Response('Hello world!');
}