Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
7cb6fe908b | |||
55624f9b67 | |||
3dd054b298 |
@ -7,12 +7,20 @@ use itguild\forms\inputs\Select;
|
|||||||
|
|
||||||
class SelectBuilder
|
class SelectBuilder
|
||||||
{
|
{
|
||||||
|
public static function build(string $name, array $params = []): Select
|
||||||
public static function build(string $name, array $params = [])
|
|
||||||
{
|
{
|
||||||
$value = $params['value'] ?? null;
|
$value = $params['value'] ?? null;
|
||||||
unset($params['value']);
|
unset($params['value']);
|
||||||
$options = $params['options'] ?? [];
|
if (isset($params['prompt'])) {
|
||||||
|
$options['prompt'] = $params['prompt'];
|
||||||
|
foreach ($params['options'] as $key => $val) {
|
||||||
|
$options[$key] = $val;
|
||||||
|
}
|
||||||
|
unset($params['prompt']);
|
||||||
|
} else {
|
||||||
|
$options = $params['options'] ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
unset($params['options']);
|
unset($params['options']);
|
||||||
|
|
||||||
return new Select(name: $name, options: $options, value: $value, paramsArray: $params);
|
return new Select(name: $name, options: $options, value: $value, paramsArray: $params);
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace itguild\forms\traits;
|
namespace itguild\forms\traits;
|
||||||
|
|
||||||
|
use itguild\forms\debug\Debug;
|
||||||
|
|
||||||
trait CreateOption
|
trait CreateOption
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -12,15 +14,37 @@ trait CreateOption
|
|||||||
public function createOption(array $options, $value = null): string
|
public function createOption(array $options, $value = null): string
|
||||||
{
|
{
|
||||||
$optionsString = "";
|
$optionsString = "";
|
||||||
if (is_array($value)) {
|
if ($value) {
|
||||||
foreach ($options as $val => $title) {
|
if (isset($options['prompt'])) {
|
||||||
$selected = in_array($title, $value) ? "selected" : "";
|
$prompt = $options['prompt'];
|
||||||
$optionsString .= "<option $selected value='$val'>$title</option>";
|
$optionsString .= "<option value='''>$prompt</option>";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($value)) {
|
||||||
|
foreach ($options as $val => $title) {
|
||||||
|
if ($val === 'prompt') continue;
|
||||||
|
$selected = in_array($title, $value) ? "selected" : "";
|
||||||
|
$optionsString .= "<option $selected value='$val'>$title</option>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ($options as $val => $title) {
|
||||||
|
if ($val === 'prompt') continue;
|
||||||
|
if (is_numeric($value)) {
|
||||||
|
$selected = (int)$value === $val ? "selected" : "";
|
||||||
|
} else {
|
||||||
|
$selected = $value === $val ? "selected" : "";
|
||||||
|
}
|
||||||
|
$optionsString .= "<option $selected value='$val'>$title</option>";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (isset($options['prompt'])) {
|
||||||
|
$prompt = $options['prompt'];
|
||||||
|
$optionsString .= "<option value='' selected='selected'>$prompt</option>";
|
||||||
|
}
|
||||||
foreach ($options as $val => $title) {
|
foreach ($options as $val => $title) {
|
||||||
$selected = (int)$value === $val ? "selected" : "";
|
if ($val === 'prompt') continue;
|
||||||
$optionsString .= "<option $selected value='$val'>$title</option>";
|
$optionsString .= "<option value='$val'>$title</option>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user