树枝和布尔

时间:2013-03-20 16:10:09

标签: symfony if-statement twig

我有一个像这样的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]">

如果gradient = false,则不检查复选框(良好),值为“1”

<input id="form_gradient" type="checkbox" value="1" required="required" name="form[gradient]">

问题是渐变的值,它就像渐变是真的:所以colorgradient字段总是显示

谢谢

1 个答案:

答案 0 :(得分:5)

我还没有测试过,但请尝试

{% if form.gradient.vars.checked %}
    <div id="gradient_color">
        {{ form_row(form.colorGradient) }}
    </div>
{% endif %}