我有一個Image類是這樣的:在我的控制器樹枝和布爾
class Image {
private $color;
private $name;
private $gradient;
private $colorGradient;
function __construct() {
$this->color = "FFFFFF";
$this->name = "blanc";
$this->gradient = false;
$this->colorGradient = "";
}
我有這樣的:
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]">
如果梯度=假的複選框未被選中(好)與值=「1」
<input id="form_gradient" type="checkbox" value="1" required="required" name="form[gradient]">
問題是任何梯度的值,它像梯度爲真:所以colorgradient字段是始終顯示
謝謝
什如果在比較中省略「true」,則會發生這種情況,將它寫爲「{%if form.gradient%}」? – raina77ow 2013-03-20 16:19:11