Skip to content

Commit a285406

Browse files
committed
fix select value
1 parent 256501d commit a285406

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/fields/FormSelect.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function __construct($key, $id, $name, $value, $label=null, array $attr=[
2828
}
2929

3030
parent::__construct($key, $id, $name, $value, null, $label, $attr);
31+
32+
/* FormInput sets value which select shouldn't have */
33+
unset($this->attr['value']);
3134
}
3235

3336
public function getOptions(){
@@ -40,6 +43,8 @@ public function getValue(){
4043

4144
public function getContent(array $extra_attr = array()){
4245
$attr = array_merge_recursive($extra_attr, $this->attr);
43-
return '<select ' . $this->serializeAttr($attr) . ">\n" . $this->options->serializeOptions($this->selected) . "\n</select>\n";
46+
$sattr = $this->serializeAttr($attr, ['name', 'id']);
47+
$soptions = $this->options->serializeOptions($this->selected);
48+
return "<select {$sattr}>\n{$soptions}\n</select>";
4449
}
4550
}

src/lib/FormOptions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getOptions(){
5353
public function serializeOptions($selected){
5454
return implode("\n", array_map(function($cur) use ($selected) {
5555
$attr = ['value' => $cur->value];
56-
if ( $cur->value === $selected ){
56+
if ( $cur->value == $selected ){
5757
$attr['selected'] = true;
5858
}
5959
$sattr = FormUtils::serializeAttr($attr);

tests/fields/FormSelectTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
<?php
22

33
use NitroXy\PHPForms\Form;
4+
use NitroXy\PHPForms\FormSelect;
45
use NitroXy\PHPForms\FormOptions;
56

67
class FormSelectTest extends PHPUnit_Framework_TestCase {
8+
public function testSelectSimple(){
9+
$field = new FormSelect('key', 'id', 'name', '7', 'label', []);
10+
$this->assertEquals("<select name=\"name\" id=\"id\">\n\n</select>", $field->getContent());
11+
}
12+
13+
public function testSelectValues(){
14+
$field = new FormSelect('key', 'id', 'name', '7', 'label', ['options' => FormOptions::fromArray([1,2,3])]);
15+
$this->assertEquals("<select name=\"name\" id=\"id\">\n<option value=\"0\">1</option>\n<option value=\"1\">2</option>\n<option value=\"2\">3</option>\n</select>", $field->getContent());
16+
}
17+
18+
public function testSelectSelected(){
19+
$field = new FormSelect('key', 'id', 'name', '1', 'label', ['options' => FormOptions::fromArray([1,2,3])]);
20+
$this->assertEquals("<select name=\"name\" id=\"id\">\n<option value=\"0\">1</option>\n<option value=\"1\" selected>2</option>\n<option value=\"2\">3</option>\n</select>", $field->getContent());
21+
}
22+
23+
public function testSelectExplicit(){
24+
$field = new FormSelect('key', 'id', 'name', '7', 'label', ['options' => FormOptions::fromArray([1,2,3]), 'selected' => 2]);
25+
$this->assertEquals("<select name=\"name\" id=\"id\">\n<option value=\"0\">1</option>\n<option value=\"1\">2</option>\n<option value=\"2\" selected>3</option>\n</select>", $field->getContent());
26+
}
27+
728
public function testNull(){
829
$mock = new MockLayout();
930
$form = Form::create('id', function($f){

0 commit comments

Comments
 (0)