symfony 2 jquery bootstrap 标签输入
symfony 2 jquery bootstrap tag input
我有两个实体:多对多关系中的 BlogPost 和关键字。我必须使用表单同时在数据库中添加 BlogPost 和关键字。我想使用像 Bootstrap tagsinput 这样的 jquery 标签输入插件在输入中插入关键字。请问我该如何实施?有我的实体:
class BlogPost
{
//...
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\ManyToMany(targetEntity="ESGISGabon\PostBundle\Entity\Keyword", cascade={"persist"})
*/
private $keywords;
public function __construct()
{
$this->keywords = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getKeywords()
{
return $this->keywords;
}
}
class Keyword
{
/**
* @var string
* @ORM\Column(type="string", name="category_title", nullable=false, length=30)
*/
protected $title;
public function getTitle()
{
return $this->title;
}
}
我终于找到了解决问题的方法。我不得不使用 Data transformer and a custom form field type。我还使用 FPNTagBundle 及其 tagManager。我是这样做的:
class TagsTransformer implements DataTransformerInterface
{
private $tagManager;
public function __construct(TagManager $tagManager)
{
$this->tagManager = $tagManager;
}
/**
* Transforms a value from the original representation to a transformed representation.
*
* By convention, transform() should return an empty string if NULL is
* passed.
*
* @param mixed $value The value in the original representation
*
* @return mixed The value in the transformed representation
*
* @throws TransformationFailedException When the transformation fails.
*/
public function transform($tags)
{
if(!is_null($tags))
return join(', ', $tags->toArray());
return '';
}
/**
* Transforms a value from the transformed representation to its original
* representation.
*
* By convention, reverseTransform() should return NULL if an empty string
* is passed.
*
* @param mixed $value The value in the transformed representation
*
* @return mixed The value in the original representation
*
* @throws TransformationFailedException When the transformation fails.
*/
public function reverseTransform($tags)
{
if(is_null($tags) || !$tags)
return;
return $this->tagManager->loadOrCreateTags(
$this->tagManager->splitTagNames($tags)
);
}
}
class TagType extends AbstractType
{
protected $tagManager;
public function __construct(TagManager $tagManager)
{
$this->tagManager = $tagManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new TagsTransformer($this->tagManager);
$builder->addModelTransformer($transformer);
}
public function getName()
{
return 'tags';
}
public function getParent()
{
return 'text';
}
}
最后我的表格是这样的:
class CorporatePostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('keywords', 'tags');
}
//.....
}
您必须为自定义表单字段类型创建一个自定义模板,以便集成 Bootstrap 标签输入。
我有两个实体:多对多关系中的 BlogPost 和关键字。我必须使用表单同时在数据库中添加 BlogPost 和关键字。我想使用像 Bootstrap tagsinput 这样的 jquery 标签输入插件在输入中插入关键字。请问我该如何实施?有我的实体:
class BlogPost
{
//...
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\ManyToMany(targetEntity="ESGISGabon\PostBundle\Entity\Keyword", cascade={"persist"})
*/
private $keywords;
public function __construct()
{
$this->keywords = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getKeywords()
{
return $this->keywords;
}
}
class Keyword
{
/**
* @var string
* @ORM\Column(type="string", name="category_title", nullable=false, length=30)
*/
protected $title;
public function getTitle()
{
return $this->title;
}
}
我终于找到了解决问题的方法。我不得不使用 Data transformer and a custom form field type。我还使用 FPNTagBundle 及其 tagManager。我是这样做的:
class TagsTransformer implements DataTransformerInterface
{
private $tagManager;
public function __construct(TagManager $tagManager)
{
$this->tagManager = $tagManager;
}
/**
* Transforms a value from the original representation to a transformed representation.
*
* By convention, transform() should return an empty string if NULL is
* passed.
*
* @param mixed $value The value in the original representation
*
* @return mixed The value in the transformed representation
*
* @throws TransformationFailedException When the transformation fails.
*/
public function transform($tags)
{
if(!is_null($tags))
return join(', ', $tags->toArray());
return '';
}
/**
* Transforms a value from the transformed representation to its original
* representation.
*
* By convention, reverseTransform() should return NULL if an empty string
* is passed.
*
* @param mixed $value The value in the transformed representation
*
* @return mixed The value in the original representation
*
* @throws TransformationFailedException When the transformation fails.
*/
public function reverseTransform($tags)
{
if(is_null($tags) || !$tags)
return;
return $this->tagManager->loadOrCreateTags(
$this->tagManager->splitTagNames($tags)
);
}
}
class TagType extends AbstractType
{
protected $tagManager;
public function __construct(TagManager $tagManager)
{
$this->tagManager = $tagManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new TagsTransformer($this->tagManager);
$builder->addModelTransformer($transformer);
}
public function getName()
{
return 'tags';
}
public function getParent()
{
return 'text';
}
}
最后我的表格是这样的:
class CorporatePostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('keywords', 'tags');
}
//.....
}
您必须为自定义表单字段类型创建一个自定义模板,以便集成 Bootstrap 标签输入。