Skip to content

Commit f66fd34

Browse files
committed
add default_class and check attributes is array
1 parent de77bc0 commit f66fd34

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/FormLib.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
class FormLib
99
{
10+
public static $default_class = '';
1011

1112
public static function __callStatic($name, $arguments)
1213
{
@@ -86,6 +87,9 @@ public static function submit($value, $attributes = [])
8687
*/
8788
public static function text($name, $value, $attributes = [])
8889
{
90+
if (!isset($attributes['class']) and '' != self::$default_class) {
91+
$attributes['class'] = self::$default_class;
92+
}
8993
return self::input('text', $name, $value, $attributes);
9094
}
9195

@@ -138,6 +142,10 @@ public static function radio($name, $value, $attributes = [])
138142
*/
139143
public static function select($name, $value, $options, $attributes = [])
140144
{
145+
if (!isset($attributes['class']) and '' != self::$default_class) {
146+
$attributes['class'] = self::$default_class;
147+
}
148+
141149
$html = sprintf(
142150
'<select id="%s" name="%s"%s>',
143151
$name,
@@ -189,11 +197,13 @@ public static function textarea($name, $value, $attributes = [])
189197
public static function attributesToHtml($attributes)
190198
{
191199
$html = '';
192-
foreach ($attributes as $attribute => $attribute_value) {
193-
if (true === $attribute_value) {
194-
$attribute_value = $attribute;
200+
if (is_array($attributes)) {
201+
foreach ($attributes as $attribute => $attribute_value) {
202+
if (true === $attribute_value) {
203+
$attribute_value = $attribute;
204+
}
205+
$html .= sprintf(' %s="%s"', $attribute, htmlspecialchars($attribute_value));
195206
}
196-
$html .= sprintf(' %s="%s"', $attribute, htmlspecialchars($attribute_value));
197207
}
198208
return $html;
199209
}

0 commit comments

Comments
 (0)