From 0d1fc432129dcc533496249f61b4c20068f36964 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Fri, 3 Feb 2017 06:35:23 +1100 Subject: [PATCH] Add isSelected() helper to form field Fixes an issue where dropdown NULL and 0 are treated as the same Fixes #2612 --- modules/backend/classes/FormField.php | 14 ++++++++++++++ .../form/partials/_field_balloon-selector.htm | 2 +- .../widgets/form/partials/_field_checkbox.htm | 2 +- .../widgets/form/partials/_field_dropdown.htm | 4 ++-- .../backend/widgets/form/partials/_field_radio.htm | 2 +- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 36a0dab75..4be372c04 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -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) diff --git a/modules/backend/widgets/form/partials/_field_balloon-selector.htm b/modules/backend/widgets/form/partials/_field_balloon-selector.htm index 20912519d..ffb725d45 100644 --- a/modules/backend/widgets/form/partials/_field_balloon-selector.htm +++ b/modules/backend/widgets/form/partials/_field_balloon-selector.htm @@ -9,7 +9,7 @@ getAttributes() ?>> diff --git a/modules/backend/widgets/form/partials/_field_checkbox.htm b/modules/backend/widgets/form/partials/_field_checkbox.htm index 962501a34..5fdd87766 100644 --- a/modules/backend/widgets/form/partials/_field_checkbox.htm +++ b/modules/backend/widgets/form/partials/_field_checkbox.htm @@ -11,7 +11,7 @@ name="getName() ?>" value="1" previewMode ? 'disabled="disabled"' : '' ?> - value == 1 ? 'checked="checked"' : '' ?> + isSelected() ? 'checked="checked"' : '' ?> getAttributes() ?>>