Add isSelected() helper to form field

Fixes an issue where dropdown NULL and 0 are treated as the same
Fixes #2612
This commit is contained in:
Samuel Georges 2017-02-03 06:35:23 +11:00
parent 3365aee129
commit 0d1fc43212
5 changed files with 19 additions and 5 deletions

View File

@ -351,6 +351,20 @@ class FormField
return $this;
}
/**
* Determine if the provided value matches this field's value.
* @param string $value
* @return bool
*/
public function isSelected($value = true)
{
if ($this->value === null) {
return false;
}
return (string) $value === (string) $this->value;
}
/**
* Sets the attributes for this field in a given position.
* - field: Attributes are added to the form field element (input, select, textarea, etc)

View File

@ -9,7 +9,7 @@
<?= $field->getAttributes() ?>>
<ul>
<?php foreach ($fieldOptions as $value => $text): ?>
<li data-value="<?= e($value) ?>" class="<?= $value == $field->value ? 'active' : '' ?>"><?= e(trans($text)) ?></li>
<li data-value="<?= e($value) ?>" class="<?= $field->isSelected($value) ? 'active' : '' ?>"><?= e(trans($text)) ?></li>
<?php endforeach ?>
</ul>

View File

@ -11,7 +11,7 @@
name="<?= $field->getName() ?>"
value="1"
<?= $this->previewMode ? 'disabled="disabled"' : '' ?>
<?= $field->value == 1 ? 'checked="checked"' : '' ?>
<?= $field->isSelected() ? 'checked="checked"' : '' ?>
<?= $field->getAttributes() ?>>
<label for="<?= $field->getId() ?>">

View File

@ -22,10 +22,10 @@
if (!is_array($option)) $option = [$option];
?>
<option
<?= $value == $field->value ? 'selected="selected"' : '' ?>
<?= $field->isSelected($value) ? 'selected="selected"' : '' ?>
<?php if (isset($option[1])): ?>data-<?=strpos($option[1],'.')?'image':'icon'?>="<?= $option[1] ?>"<?php endif ?>
value="<?= $value ?>"
><?= e(trans($option[0])) ?></option>
<?php endforeach ?>
</select>
<?php endif?>
<?php endif ?>

View File

@ -19,7 +19,7 @@
name="<?= $field->getName() ?>"
value="<?= $value ?>"
type="radio"
<?= $field->value == $value ? 'checked="checked"' : '' ?>
<?= $field->isSelected($value) ? 'checked="checked"' : '' ?>
<?= $this->previewMode ? 'disabled="disabled"' : '' ?>
<?= $field->getAttributes() ?>>