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

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


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?</blockquote>

Answers (2)


Greg SCHURGAST answers:

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


Arturo Linares answers:

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:


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