我有一个像这样的图像类:
class Image {
private $color;
private $name;
private $gradient;
private $colorGradient;
function __construct() {
$this->color = "FFFFFF";
$this->name = "blanc";
$this->gradient = false;
$this->colorGradient = "";
}
在我的 Controller 中我有这个:
public function indexAction() {
$image = new Image();
$form = $this->createFormBuilder($image)
->add('color', 'text')
->add('name', 'text')
->add('gradient', 'checkbox')
->add('colorGradient', 'text')
->getForm();
return $this->render('CramifImageBuilderBundle:Builder:index.html.twig', array('form' => $form->createView()));
}
在 index.html.twig 我有这个:
<form action="{{ path('cramif_image_builder_image_new') }}" method="post" {{ form_enctype(form) }}>
{{ form_errors(form) }}
{{form_row(form.color)}}
{{form_row(form.name)}}
<div id="gradient">
{{form_row(form.gradient)}}
</div>
{% if form.gradient == true %}
<div id="gradient_color">
{{form_row(form.colorGradient)}}
</div>
{% endif %}
<input id="submit" type='submit' value='Créer' />
</form>
如果 gradient = true 复选框被选中(好),值为'1'
<input id="form_gradient" type="checkbox" checked="checked" value="1" required="required" name="form[gradient]">
如果 gradient = false 复选框未被选中(良好)且值 = '1'
<input id="form_gradient" type="checkbox" value="1" required="required" name="form[gradient]">
问题是无论渐变的值是多少,它就像渐变是真的:所以颜色渐变字段总是显示
谢谢
最佳答案
我还没有测试过这个,但是试试
{% if form.gradient.vars.checked %}
<div id="gradient_color">
{{ form_row(form.colorGradient) }}
</div>
{% endif %}
关于symfony - Twig 和 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15528888/