不能在树枝中循环form_row [英] Can't loop form_row in twig

查看:163
本文介绍了不能在树枝中循环form_row的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



代码:

 

我想在SF2 / Twig中循环Form_row: {%用于发布帖子%}
{{form_row(formreply.body)}}
{%endfor%}

我有一个时间表怎么多少许多帖子但是它向我显示了时间表上第一个状态的表单,



p>另一个例子:

  {%for i in 0..10%} 
{{form_row(formreply .body)}}
{%endfor%}

它应该向我显示Form_Row Ten次,对吧?

它只显示一次。



note'我已经返回了formreply在控制器中

解决方案

不能像这样循环form_row。表格行只能呈现一次。如果您尝试为每个PostReply多次创建相同的表单并将它们呈现在循环中 - 它将不会再工作,因为您将获得相同的ID和字段名称。

我假设您有多篇文章,并且您希望以时间线风格显示这些文章,并在每篇文章旁边显示回复字段。为了达到这个目的,我建议你创建PostReply实体和PostReplyType(表单类型)。正如我刚才所说,你必须使用动态名称生成。



这应该给你一个你应该去哪个方向的想法:

  class Post 
{
private $ id;
private $ title;
}

类PostReply
{
private $ id;
private $ postId;
private $ message;
}

class PostReplyType extends AbstractType
{
private $ name ='reply_form';

public function setName($ name){
$ this-> name = $ name;
}
//生成器和其他所需的代码
}

然后你可以在你的控制器中做这样的事情:

  $ posts = $ postsRepository-> findAll(); 
$ postReplyForms = new ArrayCollection();

foreach($ posts为$ post){
$ postReply = new PostReply();
$ postReplyType = new PostReplyType();
$ postReplyType-> setName('reply_form_'。$ post-> getId());
$ form = $ this-> createForm($ postReplyType,$ postReply);
$ postReplyForms-> add($ form);

在树枝上:



<
{{form_start(form)}}
{{form_widget(form)}} $ b $ {{form_end (表单)}}
{%endfor%}

并命名为:

 < form> 
< input type =textid =reply_form_1_fieldname =reply_form_1 [field]/>
< / form>
< form>
< input type =textid =reply_form_2_fieldname =reply_form_2 [field]/>
< / form>

Symfony2格式是框架非常复杂的部分,我建议从文档开始, SF2形成工作。然后搜索更多用例。祝你好运。



http:/ /symfony.com/doc/current/book/forms.html


I Want To Loop a Form_row In SF2/Twig :

The Code:

{% for post in posts %}
            {{ form_row(formreply.body) }}
{% endfor %}

I Have A timeline How contient Many Post But It show me the Form Only for the first Status on timeline ,

UPDATE :

The problem is not in the Posts loop ::

An other example :

{% for i in 0..10 %}
    {{ form_row(formreply.body) }}
{% endfor %}

It should show Me the Form_Row Ten times , right ?

It show it for me only one time ..

note 'I have returned the formreply In the Controller'

解决方案

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 :

<form>
<input type="text" id="reply_form_1_field" name="reply_form_1[field]"/>
</form>
<form>
<input type="text" id="reply_form_2_field" name="reply_form_2[field]"/>
</form>

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.

http://symfony.com/doc/current/book/forms.html

这篇关于不能在树枝中循环form_row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆