mlwacosmos
mlwacosmos

Reputation: 4561

twig and boolean

I have an Image class like this :

class Image {
private $color;
private $name;
private $gradient;
private $colorGradient;

function __construct() {
    $this->color = "FFFFFF";
    $this->name = "blanc";
    $this->gradient = false;
    $this->colorGradient = "";
}

in my controller I have this :

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()));
}

in index.html.twig I have this :

<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>

if gradient = true the checkbox is checked (good) with value='1'

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

if gradient = false the checkbox is not checked (good) with value = '1'

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

The problem is whatever the value of gradient, it like gradient is true : so the colorgradient field is always displayed

Thank you

Upvotes: 2

Views: 6718

Answers (1)

Adam Elsodaney
Adam Elsodaney

Reputation: 7818

I haven't tested this yet, but try

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

Upvotes: 7

Related Questions