php 如何使 form_rest() 不显示带有 Symfony2 的字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10570002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:29:23  来源:igfitidea点击:

How to make form_rest() not display a field with Symfony2?

phpformssymfonytwig

提问by Ilan Coulon

I've started to use Symfony2 but I've some problems. I wanted to render fields by hand but it doesn't work because my field yet rendered by me is displayed with the form_rest()function too, so I have two same fields.

我已经开始使用 Symfony2 但我遇到了一些问题。我想手动渲染字段,但它不起作用,因为我尚未渲染的字段也与该form_rest()函数一起显示,所以我有两个相同的字段。

Here is my code :

这是我的代码:

<div>
     {{ form_errors(form.contenu) }}
     <textarea id="{{ form.contenu.vars.id }}" name="{{ form.contenu.vars.full_name }}">{{ form.contenu.vars.value }}</textarea>
</div>

And, at the form's end, I must put this :

而且,在表格的最后,我必须把这个:

{{ form_rest(form) }}

But it displays the "contenu" field :(

但它显示“contenu”字段:(

Do you have an idea of what's the problem ?

你知道有什么问题吗?

回答by Francesc Rosas

Another option is to explicitly mark the field as rendered:

另一种选择是将字段显式标记为已呈现:

{% do form.contenu.setRendered %}

回答by Andresch Serj

Another in my opinionless hacky way to do it is this:

在我看来,另一种不那么hacky的方法是这样的:

{{ form_widget(form._token) }} // render the CSRF Token
{{ form_end(form, {'render_rest': false}) }} // do not render anything else

It's from the official documentation(v3.0) so it's pretty much best practisei guess.

它来自官方文档(v3.0),所以我猜这几乎是最佳实践

回答by gremo

{{ form_rest(form) }}goes at the very end, after rendering each field "manually". If you are using it for the CSRF token you can always render it with:

{{ form_rest(form) }}在“手动”渲染每个字段之后,最后进行。如果您将它用于 CSRF 令牌,您可以始终使用以下方式渲染它:

{# Token CSRF #}
{{ form_widget(form._token) }}

回答by Jovan Perovic

The situation in which you don't want to show some field suggests badly designed form. You could feed some argument(s) into it's __constructto make it conditional (say, include/exclude some fields) or you could just create separate Formclasses (which is, in my opinion, a bit overkill).

您不想显示某些字段的情况表明表单设计不当。您可以向其中提供一些参数__construct以使其成为有条件的(例如,包含/排除某些字段),或者您可以只创建单独的Form类(在我看来,这有点矫枉过正)。

I had common case few months ago when form differed when user inserted/updated records. It was something like this:

几个月前,当用户插入/更新记录时表单不同时,我遇到了常见的情况。它是这样的:

...
public function __construct($isUpdateForm){
    $this->isUpdateForm= $isUpdateForm;
}

public function buildForm(FormBuilder $builder, array $options){
    ....
    $builder->add('some_filed', 'text', ..... );

    if ( $this->isUpdateForm ){
        $builder->add(.....);
    }
    ....
}
....

If for some reasons you're not able to refactor form class you could still display unwanted fields but wrap them into <div>which has CSS display:noneattribute. That way "they are still there" (and by all means are processed normally) but are not visible to user.

如果由于某些原因您无法重构表单类,您仍然可以显示不需要的字段,但将它们包装到<div>具有 CSSdisplay:none属性的地方。这样“它们仍然存在”(并且无论如何都正常处理)但对用户不可见。

Hope this helps...

希望这可以帮助...