logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
Is there a way to feed an array to checkbox_tag in Symfony 1.0?

A friend sends me this question. I do not know enough about Symfony 1.0 to be able to answer him, so I repost it here:


Looking through the Symfony docs, I'm not seeing a beautiful way to feed the checkbox_tag a nice array or variable that it can use to preselect the right one automatically. Instead, it's looking like I'll need to do something like

<?php echo checkbox_tag('single', 1, $singlepre) ?>
<?php echo checkbox_tag('married', 2, $marriedpre) ?>
<?php echo checkbox_tag('divorced', 3, $divorcedpre) ?>

Those pre-variables would be set in the action, of course.

Or, looking at it, I could do something where I set all the values in an array, then iterate through it. But the problem with that is, I'd need two arrays, wouldn't I -- the array containing the list of all possible values, and the array containing the list of the values in this record. I wonder if the other method wouldn't, in the end, be clearer and easier to understand.

Or is there some elegant way of doing this in Symfony that I just don't get?

This question has been answered.

Lawrence Krubner | 06/23/10 at 3:16pm Edit


(2) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/23/10
    6:11pm
    Greg SCHURGAST says:

    you should iterate an array like that :

    In action you can define an array :
    $this->checkboxes = array ('single','married','divorced');

    then in template:

    foreach ($checkboxes as $k => $checkboxe){
    $val = $checkboxe . 'pre';
    echo checkbox_tag($checkboxe, $k+1, $$val);
    }


    (notice the $$ in the loop)
    This is not link to symfony but php

  • avatar
    Last edited:
    06/23/10
    6:11pm
    Arturo Linares says:

    Not out of the box. You can create a helper to read $sf_params and determine the default value (if you have to prepopulate them from the request), something like the code below; you'll still need to iterate though.

    I know someone created a plugin to use the forms framework used in sf1.1 but I don't know if the thing is usable, it is abandoned though: http://www.symfony-project.org/plugins/sfForms11Plugin


    <?

    function checkbox_tag_extended($name, $label, $checked = false, $extra_options) {
    $sf_request = sfContext::getInstance()->getRequest();

    $checked = $sf_request->hasParameter($name);
    ?>

    <label for="<?= $name ?>"><?= $label ?></label>
    <?= checkbox_tag($name, true, $checked, $extra_options) ?>
    <?
    }

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.