jbrinley / wp-forms

An API for creating and processing forms in WordPress
101 stars 10 forks source link

How to add selected attribute to specific options in a checkbox group. #8

Open richardsweeney opened 10 years ago

richardsweeney commented 10 years ago

I can create a checkbox group like so:

$checkboxes = WP_Form_Element::create( 'checkboxes' )
    ->set_name( 'interests' )
    ->set_label( 'Interests' )
    ->add_option( 'something,'Something' )
    ->add_option( 'something_else', Something else' );

Cool.

But if I'm grabbing data from the database (after the form has been submitted), how do I show which options have been selected.

$checkboxes->set_attribute( 'checked', 'checked' );

Will sets all the attributes to checked. There doesn't seem to be anything I can find in the docs about this. Any tips!?

jbrinley commented 10 years ago

You should be able to use $checkboxes->set_default_value(array('something'));, just as you do with select boxes. But quickly skimming the code in WP_Form_View_Checkboxes, it looks like I haven't yet implemented that. I'll add it in as soon as I can (but I'm a little overwhelmed right now, so pull requests are always welcome).

richardsweeney commented 10 years ago

Cool. I'll take a look and see if I can fix this myself. It's such a fantastic project I'd love to contribute if I can :)

dentm42 commented 9 years ago

Richard - THANK YOU for the update! I was pulling my hair out for a bit.

In case anyone else needs this fixed - see Richard's pull request:

https://github.com/jbrinley/wp-forms/pull/9

korobochkin commented 8 years ago

Oh. I tried to solved this on the past week but can't create a pretty working solution and now I find this. You rock @richardsweeney. Thank you.