Symfony - 将项目作为纯文本 [英] Symfony - Form item as plain text

查看:149
本文介绍了Symfony - 将项目作为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,包含一组表单(一个投票和许多 VoteChoice )。 VoteChoiceType 如下

I have a form that contains a collection of forms (a Vote with many VoteChoice). The VoteChoiceType is as follows

class VoteChoiceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('answer', null, array('disabled' => true))
            ->add('priority', null);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'PollBundle\Entity\VoteChoice',
        ));
    }
}

现在在我的控制器中,我创建并填充了许多 VoteChoices ,根据当前投票的可用选项(从URL导出)设置答案

Now in my Controller I create and populate many VoteChoices, setting the answer according to the available choices for the current poll (derived from the URL)

$vote = new Vote();
$vote->setPoll($poll);
foreach ($vote->getPoll()->getPollOptions() as $op) {
    $vc = New VoteChoice();
    $vote->addVoteChoice($vc->setAnswer($op));
}

所以当窗体加载时,我希望所有选项只显示 - 不作为实际选择,然后用户可以设置他们想要的优先级。但是,答案是在我的 poll_options 表(每个投票中有很多 PollOption ,类似于每个投票有很多 VoteChoice

So when the Form loads, I want all the options to display only - not to be an actual choice, and then the user can set the priority they want. However, the answer is of every single answer I have in my poll_options table (each Poll has many PollOption, similar to how each Vote has many VoteChoice)

当前的树枝模板

<ul class="voteChoices" data-prototype="{{ form_widget(form.voteChoices.vars.prototype)|e('html_attr')  }}">
    {% for voteChoice in form.voteChoices %}
            <li>{{ form_row(voteChoice.answer) }} {{ form_row(voteChoice.priority) }}</li>
    {% endfor %}
</ul>
</div>
<p><button type="submit" class="btn btn-success">Go!</button></p>
{{ form_end(form) }}

我想把voteChoice.answer作为一个简单文本(所以它不是下拉菜单的一部分 - 我知道我可以在FormBuilder中禁用它,但我不希望它作为下拉菜单的一部分出现,我只是想将其作为纯文本)

I want the voteChoice.answer as a plain text (so it's not part of a dropdown - I know I can disable it in the FormBuilder, but I don't want it to appear as part of a drop-down menu, I just want it as plain text)

如果我使用voteChoice.answer,我得到以下symfony错误

If I use voteChoice.answer I get the following symfony error


已经抛出异常在模板的渲染过程中(第9行)的poll\vote.html.twig中的Catchable致命错误:类Symfony\Component\Form\FormView的对象无法转换为字符串)。

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Symfony\Component\Form\FormView could not be converted to string") in poll\vote.html.twig at line 9.

我的 VoteChoice 类中有一个__toString函数。

I have a __toString function in my VoteChoice class.

推荐答案


我想把voteChoice.answer作为一个纯文本(所以它不是下拉菜单的一部分 - 我知道我可以禁用它FormBuilder,但是我不希望它作为下拉菜单的一部分出现,我只是想要它ain文本)

I want the voteChoice.answer as a plain text (so it's not part of a dropdown - I know I can disable it in the FormBuilder, but I don't want it to appear as part of a drop-down menu, I just want it as plain text)

您可以通过 form.vars.value 参考):

{{ voteChoice.vars.value.answer }}

这意味着 voteChoice.vars.value PollBundle\Entity\VoteChoice 因此,如果编辑不需要,您可以安全地从您的表单中删除答案字段。

This means that voteChoice.vars.value is an instance of PollBundle\Entity\VoteChoice so you can remove the answer field from your form safely if this is not required by edit.

这篇关于Symfony - 将项目作为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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