t loop 传递html,html - Can't loop form_row in twig - Stack Overflow

You cannot loop a form_row like that. Form row can be rendered only once. If you try to create same form multiple times for each PostReply and render them in a loop - it won't work again, because you will get same ID's and field names.

I'm assuming you have collection of posts, and you want to show them in the timeline style, with reply field rendered next to each post. To achieve that I suggest you create PostReply entity and PostReplyType (form type). As I said earlier you have to use dynamic name generation.

This should give you an idea in which direction you should go:

class Post

{

private $id;

private $title;

}

class PostReply

{

private $id;

private $postId;

private $message;

}

class PostReplyType extends AbstractType

{

private $name = 'reply_form';

public function setName($name){

$this->name = $name;

}

// builder and other required code

}

Then you will be able to do something like this in your controller:

$posts = $postsRepository->findAll();

$postReplyForms = new ArrayCollection();

foreach($posts as $post) {

$postReply = new PostReply();

$postReplyType = new PostReplyType();

$postReplyType->setName('reply_form_' . $post->getId());

$form = $this->createForm($postReplyType, $postReply);

$postReplyForms->add($form);

}

In twig:

{% for form in postReplyForms %}

{{ form_start(form) }}

{{ form_widget(form) }}

{{ form_end(form) }}

{% endfor %}

This should render forms with dynamic ID's and names as :

Symfony2 forms are very complex part of the framework, I recommend beginning with documentation to get basic understanding of how SF2 forms work. Then googling for more use cases. Good luck.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值