From 495a729db6f60fe4b0ae1d76feb99666e5265178 Mon Sep 17 00:00:00 2001 From: Richard Sweeney Date: Sat, 18 Jan 2014 13:01:45 +0100 Subject: [PATCH 1/2] Allow an array of checked values to be passed to checkboxes in order to be able to set the checked attribute to 'checked'. --- classes/elements/WP_Form_Element_Checkboxes.php | 7 +++++++ classes/views/WP_Form_View_Checkboxes.php | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/classes/elements/WP_Form_Element_Checkboxes.php b/classes/elements/WP_Form_Element_Checkboxes.php index c2ef0e9..15ceffb 100644 --- a/classes/elements/WP_Form_Element_Checkboxes.php +++ b/classes/elements/WP_Form_Element_Checkboxes.php @@ -3,4 +3,11 @@ class WP_Form_Element_Checkboxes extends WP_Form_Element_Multiple { protected $type = 'checkboxes'; protected $default_view = 'WP_Form_View_Checkboxes'; + + public function get_selected() { + if ( !empty($this->value) ) { + return $this->value; + } + return $this->default_value; + } } diff --git a/classes/views/WP_Form_View_Checkboxes.php b/classes/views/WP_Form_View_Checkboxes.php index bae80b1..6224b87 100644 --- a/classes/views/WP_Form_View_Checkboxes.php +++ b/classes/views/WP_Form_View_Checkboxes.php @@ -7,14 +7,19 @@ protected function checkboxes( WP_Form_Element_Checkboxes $element ) { throw new LogicException(__('Cannot render checkbox group without a name', 'wp-forms')); } $options = $element->get_options(); + $selected = $element->get_selected(); $output = ''; foreach ( $options as $key => $label ) { - $output .= $this->checkbox( $key, $label, $attributes ); + $checked = false; + if ( is_array( $selected ) && in_array( $key, $selected ) ) { + $checked = true; + } + $output .= $this->checkbox( $key, $label, $attributes, $checked ); } return $output; } - protected function checkbox( $key, $label, $attributes ) { + protected function checkbox( $key, $label, $attributes, $checked ) { $checkbox = WP_Form_Element::create('checkbox') ->set_name($attributes['name'].'[]') ->set_label($label) @@ -32,6 +37,9 @@ protected function checkbox( $key, $label, $attributes ) { foreach ( $attributes as $att => $value ) { $checkbox->set_attribute($att, $value); } + if ( $checked ) { + $checkbox->set_attribute('checked', 'checked'); + } return $checkbox->render(); } } From 5530e1fb1002aeb66c8b44e7f7c101d46dbb7b5a Mon Sep 17 00:00:00 2001 From: Richard Sweeney Date: Sat, 18 Jan 2014 13:08:12 +0100 Subject: [PATCH 2/2] Typecast all values as an array, in case there is a string passed as the default value. --- classes/views/WP_Form_View_Checkboxes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/views/WP_Form_View_Checkboxes.php b/classes/views/WP_Form_View_Checkboxes.php index 6224b87..738c36b 100644 --- a/classes/views/WP_Form_View_Checkboxes.php +++ b/classes/views/WP_Form_View_Checkboxes.php @@ -7,11 +7,11 @@ protected function checkboxes( WP_Form_Element_Checkboxes $element ) { throw new LogicException(__('Cannot render checkbox group without a name', 'wp-forms')); } $options = $element->get_options(); - $selected = $element->get_selected(); + $selected = (array) $element->get_selected(); $output = ''; foreach ( $options as $key => $label ) { $checked = false; - if ( is_array( $selected ) && in_array( $key, $selected ) ) { + if ( in_array( $key, $selected ) ) { $checked = true; } $output .= $this->checkbox( $key, $label, $attributes, $checked );