问题描述
我有一个表单,包含一组表单(一个投票
和许多 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
我的 VoteChoice
类中有一个__toString函数。
I have a __toString function in my VoteChoice
class.
推荐答案
您可以通过 form.vars.value $访问您的表单的当前数据c $ c>():
{{ 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 - 将项目作为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!