2

次のような 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>

グラデーション = true の場合、値 = '1' でチェックボックスがチェックされている (良い)

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

グラデーション = false の場合、値 = '1' でチェックボックスがチェックされていない (良い)

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

問題は、グラデーションの値が何であれ、グラデーションが true のようです: したがって、 colorgradient フィールドは常に表示されます

ありがとうございました

4

1 に答える 1

7

私はまだこれをテストしていませんが、試してみてください

{% if form.gradient.vars.checked %}
    <div id="gradient_color">
        {{ form_row(form.colorGradient) }}
    </div>
{% endif %}
于 2013-03-20T16:30:11.983 に答える