Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for use of select element #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
8 changes: 5 additions & 3 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand All @@ -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;
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -161,4 +163,4 @@ public function __get($property)
return $this->{"_".$property};
}

}
}
6 changes: 3 additions & 3 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= "<option{$selectedAttr}{$valueAttr}>{$value}</option>";
Expand All @@ -142,4 +142,4 @@ public static function endTag( $tag )
return "</{$tag}>";
}

}
}