如何将html元素添加到表单字段中。怎么样?下面的推理是错误的,因为它在"输入"中输出我的div。字段,而不是在表单字段之间插入纯HTML。我想要而不是输入,以显示将加载图像的HTML代码。我的意思是我在图像和用户之间存在ManyToMany关系。当我有数据转换器,它生成字符串:
"<img src=\"{{ asset( 'bundles/meeting/images/uploads/".$img->getPath()."') }}\" height=\"200\" />"
。我想在用户表单字段之间插入这些字符串,最终应显示图像。
C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \实体\ user.php的
/**
* User
*
* @ORM\Table(name="tuser")
* @ORM\Entity()
*/
class User implements AdvancedUserInterface, \Serializable
...
/**
* @ORM\ManyToMany(targetEntity="\MeetingBundle\Entity\Image")
* @ORM\JoinTable(name="tUserImgUni",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id")}
* )
*/
private $imgsuni;
....
/**
* Constructor
*/
public function __construct()
{
$this->imgsuni = new ArrayCollection();
}
...
/**
* Add imgsuni
*
* @param \MeetingBundle\Entity\Image $imgsuni
*
* @return User
*/
public function addImgsuni(\MeetingBundle\Entity\Image $image)
{
$this->imgsuni[] = $image;
return $this;
}
....
C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \实体\ Image.php
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Image
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
...
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
..
/**
* Called after entity persistence
*
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move(
'C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj3_27\web\bundles\meeting\images\uploads',
$this->path
);
$this->file = null;
}
C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \表格\ UserType.php
...
use MeetingBundle\Form\DataTransformer\ImageDataTransformer;
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $options['em'];
$trans = new ImageDataTransformer($em);
$builder
->add('username')
->add(
$builder
->create('imgsuni', 'text')
->addModelTransformer($trans)
)
...
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MeetingBundle\Entity\User'
))
->setRequired(['em'])
->setAllowedTypes('em', 'Doctrine\ORM\EntityManager')
;
}
C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \表格\ DataTransformer \ ImageDataTransformer.php
<?php
namespace MeetingBundle\Form\DataTransformer;
use MeetingBundle\Entity\Image;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;
class ImageDataTransformer implements DataTransformerInterface
{
/**
* @var EntityManager
*/
private $em;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* Transforms an objects (Tags) to a string.
*/
public function transform($imgs)
{
if (null === $imgs) {
return "";
}
$output = [];
foreach ($imgs as $img) {
$output[] =
"<img src=\"{{ asset( 'bundles/meeting/images/uploads/".$img->getPath()."') }}\" height=\"200\" />"
;
}
return join(', ', $output);
}
}
例如,我有id = 21且image id = 1的用户。 我的变压器生成字符串:
<img src="{{ asset( 'bundles/meeting/images/uploads/a4d7fb6b1282815d41e2015bfe6b3334f0833063.png') }}" height="200" />
我想要将此字符串作为html代码插入,而不是在表单的输入中显示。
C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \控制器\ UserController.php
/**
* Displays a form to edit an existing User entity.
*
* @Route("/{id}/edit", name="user_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, User $user)
{
$em = $this->getDoctrine()->getManager();
$deleteForm = $this->createDeleteForm($user);
$editForm = $this->createForm(new UserType(), $user, ['em' => $em ]);
$editForm->add('submit', 'submit', array(
'label' => 'Update details',
));
$image = new Image();
$imageForm=$this->createForm(new ImageType(), $image);
$imageForm->add('submit', 'submit', array('label' => 'Upload image'));
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
}
$imageForm->handleRequest($request);
if ($imageForm->isSubmitted() && $imageForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($image);
$em->flush();
//$image_id=$image->getId();
$user->addImgsuni($image);
$em->persist($user);
$em->flush(); //MeetingBundle::
return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
}
return $this->render('MeetingBundle::user/edit.html.twig', array(
'user' => $user,
'edit_form' => $editForm->createView(),
'image_form' => $imageForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \资源\视图\图片\ edit.html.twig
{% extends "MeetingBundle::layout.html.twig" %}
{% block body %}
<h1>User edit</h1>
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
<br>
{{ form(image_form) }}
<ul>
<li>
<a href="{{ path('user_index') }}">Back to the list</a>
</li>
<br>
<li>
{{ form_start(delete_form) }}
{{ form_end(delete_form) }}
</li>
</ul>
{% endblock %}
url:http://localhost:8000/user/21/edit
这将显示带有Imgsuni输入字段的表单,其中包含以下内容:
<img src="{{ asset( 'bundles/meeting/images/uploads/a4d7fb6b1282815d41e2015bfe6b3334f0833063.png') }}" height="200" />
答案 0 :(得分:0)
方法是将html添加到Image实体中的某个字段,称为“htmlimg
”。比使用{{ image.htmlimg }}
显示树枝模板中字段的内容。这会生成:<img src="{{ asset( 'bundles/meeting/images/uploads/a4d7fb6b1282815d41e2015bfe6b3334f0833063.png') }}" height="200" />
并显示图像。