views:

435

answers:

1

Hi,

I'm able to do the following for all checkboxes in a set (in an action):

$this->form->getWidget('some_form_field')->setAttribute('checked', 'checked');

... but I'm unable to set specific checkboxes to ticked on the basis of data returned from the db.

I'm after something like:

$this->form->getWidget('some_form_field')->setAttributes(array(....));

... where I can refer to the specific checkboxes to be ticked somehow, or pass an array to it.

There's nothing in the symfony documentation on this specifically and I've had enough of trying a dozen combinations to get it work.

Any help would be appreciated.

Thanks.

+1  A: 

This sounds a bit unusual? Are you using the generated propel/doctrine forms? They should handle it automatically.

Either way, setting the checked attribute is not the correct way to achieve this. Better way is within the form (because as far as I remember, $this->widgetSchema is protected) to do:

$this->widgetSchema['some_form_field']->setDefault(array('value1', 'value2'));

That will then cause widgets with those values to be checked.

(if thats not right, I wrote it off the top of my head, I can dig some code out and check, so let me know!)

benlumley
@benlumley... thanks but the form gets rendered "not checked" on purpose, so that when I pull data in my action from the db, I can then render the form in the template with those checkboxes pre-checked that were stored in that way in the db. As I note above, I can get it to pre-check all of them fine (both in form class and and in action) but I need control on the level of individual checkboxes.
Tom
... actually your above code is the right direction. Just got it to work: $this->form->getWidget('some_form_field')->setDefault(array('1', '2'));Thanks!
Tom
You can still achieve this by adding a method to the form to accept input and set 'checked' on the relevant checkboxes. You can then call this method from the action and pass it your data. If you need to do this in the view, which in my opinion is a bit messy, there are ways to do so as well - each widgets render method can accept an array of the current values.
benlumley
Indeed... thanks again.
Tom