Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion php/class-fieldmanager-select.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Fieldmanager_Select extends Fieldmanager_Options {
*/
public $first_empty = false;

/**
* String to use for first empty element.
*
* @var string
*/
public $first_empty_label = ' ';

/**
* Tell FM to save multiple values.
*
Expand Down Expand Up @@ -120,7 +127,7 @@ public function form_element( $value = array() ) {

$opts = '';
if ( $this->is_repeatable() || $this->first_empty ) {
$opts .= '<option value="">&nbsp;</option>';
$opts .= '<option value="">' . esc_html( $this->first_empty_label ) . '</option>';
}
$opts .= $this->form_data_elements( $value );

Expand Down
22 changes: 22 additions & 0 deletions tests/php/test-fieldmanager-select-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,28 @@ public function test_first_empty() {
);
}

public function test_first_empty_label() {
$fm = new Fieldmanager_Select(
array(
'name' => 'base_field',
'options' => array( 'one', 'two', 'three' ),
'first_empty' => true,
'first_empty_label' => '-- Empty --',
)
);

$html = $this->_get_html_for( $fm );
$this->assertRegExp(
'#<select[^>]+>'
. '\s*<option value="">-- Empty --</option>'
. '\s*<option value="one"\s*>one</option>'
. '\s*<option value="two"\s*>two</option>'
. '\s*<option value="three"\s*>three</option>'
. '\s*</select>#si',
$html
);
}

public function test_default_value() {
$fm = new Fieldmanager_Select(
array(
Expand Down