diff --git a/src/Form.php b/src/Form.php index 5091857..d1d95bc 100644 --- a/src/Form.php +++ b/src/Form.php @@ -31,6 +31,7 @@ class Form protected $model; // model to be bound protected $type; // element type: select, input, textarea protected $options; // only for select element + protected $selectedValue; // only for select element protected $useOptionKeysForValues; // only for select element public static function input( $name, $type = 'text' ) @@ -40,11 +41,12 @@ public static function input( $name, $type = 'text' ) return $elem; } - public static function select( $name, $options, $useOptionKeysForValues = false ) + public static function select( $name, $options, $selectedValue = null, $useOptionKeysForValues = false ) { $elem = static::createElement($name,"select"); $elem->options = $options; $elem->useOptionKeysForValues = $useOptionKeysForValues; + $elem->selectedValue = $selectedValue; return $elem; } @@ -98,7 +100,7 @@ protected function showInput() protected function showSelect() { - return Html::select( $this->name, $this->options, $this->attributes, $this->useOptionKeysForValues ); + return Html::select( $this->name, $this->options, $this->attributes, $this->selectedValue, $this->useOptionKeysForValues ); } protected function showTextarea() @@ -161,4 +163,4 @@ public function __get($property) return $this->{"_".$property}; } -} \ No newline at end of file +} diff --git a/src/Html.php b/src/Html.php index ec48c3f..1a43aac 100644 --- a/src/Html.php +++ b/src/Html.php @@ -112,12 +112,12 @@ public static function select( $name, $options, $attributes = [], $selectedValue foreach ( $options as $key => $value ){ if( $useKeysAsValues ) { - $selectedAttr = $key === $selectedValue ? " selected" : ""; + $selectedAttr = $key === $selectedValue ? " selected='selected'" : ""; $valueAttr = " value='{$key}'"; } else { - $selectedAttr = $value === $selectedValue ? " selected" : ""; + $selectedAttr = $value === $selectedValue ? " selected='selected'" : ""; $valueAttr = ""; } $output .= "{$value}"; @@ -142,4 +142,4 @@ public static function endTag( $tag ) return ""; } -} \ No newline at end of file +}