diff --git a/customfield/classes/data_controller.php b/customfield/classes/data_controller.php index bd19792bd5f..49acf650953 100644 --- a/customfield/classes/data_controller.php +++ b/customfield/classes/data_controller.php @@ -238,7 +238,13 @@ abstract class data_controller { */ protected function is_unique($value) : bool { global $DB; + + // Ensure the "value" datafield can be safely compared across all databases. $datafield = $this->datafield(); + if ($datafield === 'value') { + $datafield = $DB->sql_cast_to_char($datafield); + } + $where = "fieldid = ? AND {$datafield} = ?"; $params = [$this->get_field()->get('id'), $value]; if ($this->get('id')) { diff --git a/customfield/field/textarea/classes/data_controller.php b/customfield/field/textarea/classes/data_controller.php index 64e62d1fce7..3d52fe15bc4 100644 --- a/customfield/field/textarea/classes/data_controller.php +++ b/customfield/field/textarea/classes/data_controller.php @@ -136,6 +136,29 @@ class data_controller extends \core_customfield\data_controller { $instance->{$this->get_form_element_name()} = $value; } + /** + * Checks if the value is empty, overriding the base method to ensure it's the "text" element of our value being compared + * + * @param mixed $value + * @return bool + */ + protected function is_empty($value): bool { + if (is_array($value)) { + $value = $value['text']; + } + return html_is_blank($value); + } + + /** + * Checks if the value is unique, overriding the base method to ensure it's the "text" element of our value being compared + * + * @param mixed $value + * @return bool + */ + protected function is_unique($value): bool { + return parent::is_unique($value['text']); + } + /** * Delete data * @@ -166,9 +189,6 @@ class data_controller extends \core_customfield\data_controller { require_once($CFG->libdir . '/filelib.php'); $value = $this->get_value(); - if ($this->is_empty($value)) { - return null; - } if ($dataid = $this->get('id')) { $context = $this->get_context();