树枝和布尔

mlw*_*mos 2 if-statement symfony 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 = "";
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中我有这个:

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()));
}
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

如果gradient = true,则选中复选框(好),值为'1'

<input id="form_gradient" type="checkbox" checked="checked" value="1" required="required" name="form[gradient]">
Run Code Online (Sandbox Code Playgroud)

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

<input id="form_gradient" type="checkbox" value="1" required="required" name="form[gradient]">
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Ada*_*ney 5

我还没有测试过这个,但试试吧

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