Skip to content

Commit 07337e3

Browse files
author
Bradie Tilley
committed
Add default value support for select dropdowns
1 parent 8b3bfd8 commit 07337e3

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

classes/HtmlGenerator.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -516,17 +516,19 @@ public function resolveTypeRadio(Form $form, Field $field)
516516
*/
517517
public function resolveTypeSelect(Form $form, Field $field)
518518
{
519-
$el = new HtmlSelect([
520-
]);
519+
$el = new HtmlSelect([]);
521520

522521
$el->set($this->resolveTypeGlobal($form, $field));
522+
$defaultValue = $field->default_value;
523523

524-
$el->addChild(new HtmlOption([
525-
'value' => '',
526-
'selected' => true,
527-
'disabled' => $field->required,
528-
'node' => $field->placeholder,
529-
]));
524+
if (($field->placeholder !== null) && ($defaultValue === null)) {
525+
$el->addChild(new HtmlOption([
526+
'value' => '',
527+
'selected' => true, // Selected by default
528+
'disabled' => $field->required, // If the field is required, you cannot select the placeholder
529+
'node' => $field->placeholder,
530+
]));
531+
}
530532

531533
foreach ($field->getOptions() as $option) {
532534
if ($option->is_optgroup) {
@@ -536,6 +538,7 @@ public function resolveTypeSelect(Form $form, Field $field)
536538

537539
foreach ($option->options as $option) {
538540
$optgroup->addChild(new HtmlOption([
541+
'selected' => ($defaultValue !== null) ? ($option->option_code === $defaultValue) : false,
539542
'value' => $option->option_code,
540543
'node' => $option->option_label,
541544
]));
@@ -547,6 +550,7 @@ public function resolveTypeSelect(Form $form, Field $field)
547550
}
548551

549552
$el->addChild(new HtmlOption([
553+
'selected' => ($defaultValue !== null) ? ($option->option_code === $defaultValue) : false,
550554
'value' => $option->option_code,
551555
'node' => $option->option_label,
552556
]));

models/Field.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace ABWebDevelopers\Forms\Models;
1+
<?php
2+
3+
namespace ABWebDevelopers\Forms\Models;
24

35
use Model;
46
use October\Rain\Database\Traits\Sortable;
@@ -41,6 +43,7 @@ class Field extends Model
4143
'group_class',
4244
'label_class',
4345
'options',
46+
'default',
4447
];
4548

4649
/**
@@ -236,7 +239,7 @@ public function getOptionKeys(): array
236239
* @param string $key
237240
* @return string|null The option label
238241
*/
239-
public function getOption(string $key): ? string
242+
public function getOption(string $key): ?string
240243
{
241244
foreach ($this->options as $option) {
242245
if ($option['is_optgroup'] ?? false) {
@@ -254,4 +257,28 @@ public function getOption(string $key): ? string
254257

255258
return null;
256259
}
260+
261+
/**
262+
* Get the placeholder for this field
263+
*
264+
* @return mixed
265+
*/
266+
public function getPlaceholderAttribute()
267+
{
268+
return (isset($this->attributes['placeholder']) && ($this->attributes['placeholder'] !== ''))
269+
? $this->attributes['placeholder']
270+
: null;
271+
}
272+
273+
/**
274+
* Get the default value for this field
275+
*
276+
* @return mixed
277+
*/
278+
public function getDefaultValueAttribute()
279+
{
280+
return (isset($this->attributes['default']) && ($this->attributes['default'] !== ''))
281+
? $this->attributes['default']
282+
: null;
283+
}
257284
}

updates/version.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@
5050
1.4.0:
5151
- 'Added new plain text field type for adding general HTML to forms'
5252
1.5.0:
53-
- '** Remove foreign key constraints from historic migrations **'
53+
- '** Remove foreign key constraints from historic migrations **'
54+
1.5.1:
55+
- 'Add default value support for select dropdowns'

0 commit comments

Comments
 (0)