Forms 在Symfony 2中嵌入表单集合并添加多个文件
我在这里遵循教程: 该示例有效,但当我尝试添加filetype字段时,出现以下错误:Forms 在Symfony 2中嵌入表单集合并添加多个文件,forms,symfony,Forms,Symfony,我在这里遵循教程: 该示例有效,但当我尝试添加filetype字段时,出现以下错误: Error: Call to a member function move() on a non-object 不知道为什么。我可以动态添加任意多个fieldType字段,但问题在于上面的错误 这是我的密码: TagType.php namespace Bundle\MyBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfon
Error: Call to a member function move() on a non-object
不知道为什么。我可以动态添加任意多个fieldType字段,但问题在于上面的错误
这是我的密码:
TagType.php
namespace Bundle\MyBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
//$builder->add('name');
$builder->add('file','file',array('label' => 'Load file'));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Bundle\MyBundle\Entity\Tag',
));
}
public function getName()
{
return 'tag';
}
}
TaskType.php
namespace Bundle\MyBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('description');
$builder->add('file', 'collection', array(
'type' => new TagType(),
'allow_add' => true,
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Bundle\MyBundle\Entity\Task',
));
}
public function getName()
{
return 'task';
}
}
Task.php
/**
* Task
*/
class Task
{
/**
* @var string
*/
protected $description;
/**
* @var string
*/
protected $tags;
/**
* @var integer
*/
/**
* @var string
*/
protected $file;
/**
* @var string
*/
protected $path;
protected $id;
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* Set description
*
* @param string $description
* @return Task
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set tags
*
* @param string $tags
* @return Task
*/
public function setTags($tags)
{
$this->tags = $tags;
}
/**
* Get tags
*
* @return string
*/
public function getTags()
{
return $this->tags;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set file
*
* @param string $file
* @return Task
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* Get file
*
* @return string
*/
public function getFile()
{
return $this->file;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
$user_folder = $this->getId();
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/task/';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
return $this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
}
Tag.php
class Tag
{
/**
* @var string
*/
private $file;
/**
* @var string
*/
private $path;
/**
* @var string
*/
public $name;
/**
* Set name
*
* @param string $name
* @return Task
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set file
*
* @param string $file
* @return Task
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* Get file
*
* @return string
*/
public function getFile()
{
return $this->file;
}
/**
* Set path
*
* @param string $path
* @return Task
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
$user_folder = $this->getId();
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/task/';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
return $this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
}
控制器:
$task = new Task();
$form = $this->createForm(new TaskType(), $task);
$form->add('submit', 'submit', array('label' => 'Create'));
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$task->upload();
//$task->setTags($val);
$em->persist($task);
$em->flush();
$em->clear();
}