diff --git a/src/Forms/FormField.php b/src/Forms/FormField.php index d5a08e39a95..fe953afba8d 100644 --- a/src/Forms/FormField.php +++ b/src/Forms/FormField.php @@ -1493,6 +1493,16 @@ public function getSchemaDataDefaults() if ($titleTip instanceof Tip) { $data['titleTip'] = $titleTip->getTipSchema(); } + $attributes = $this->getAttributes(); + // Remove value from attributes because otherwise it breaks react fields + unset($attributes['value']); + // Remove above attributes from attributes list to avoid double-ups + foreach (array_keys($data) as $key) { + // HTML attributes are always lowercase so we need to make sure to transform our js key names + // to lowercase before unsetting them. + unset($attributes[strtolower($key)]); + } + $data['attributes'] = $attributes; return $data; } @@ -1562,6 +1572,18 @@ public function getSchemaValidation() return $validationList; } + /** + * Gets the data-schema and data-state attributes for the input element. + * Can't be included in getAttributesHtml because that would result in + * an infinite loop. + */ + public function getSchemaAttributesHtml(): DBHTMLText + { + $content = 'data-schema="' . htmlspecialchars(json_encode($this->getSchemaData())) + . '" data-state="' . htmlspecialchars(json_encode($this->getSchemaState())) . '"'; + return DBHTMLText::create()->setValue($content); + } + /** * @return Tip */ diff --git a/src/Forms/SearchableDropdownTrait.php b/src/Forms/SearchableDropdownTrait.php index fa751c49a2f..56e80821c62 100644 --- a/src/Forms/SearchableDropdownTrait.php +++ b/src/Forms/SearchableDropdownTrait.php @@ -312,7 +312,6 @@ public function getAttributes(): array parent::getAttributes(), [ 'name' => $name, - 'data-schema' => json_encode($this->getSchemaData()), ] ); } diff --git a/src/Forms/TreeDropdownField.php b/src/Forms/TreeDropdownField.php index 5bf454f5513..7a8889dafca 100644 --- a/src/Forms/TreeDropdownField.php +++ b/src/Forms/TreeDropdownField.php @@ -620,8 +620,6 @@ public function getAttributes() $attributes = [ 'class' => $this->extraClass(), 'id' => $this->ID(), - 'data-schema' => json_encode($this->getSchemaData()), - 'data-state' => json_encode($this->getSchemaState()), ]; $attributes = array_merge($attributes, $this->attributes); diff --git a/templates/SilverStripe/Forms/TreeDropdownField.ss b/templates/SilverStripe/Forms/TreeDropdownField.ss index fad2a4e5f3c..c17e1582d2b 100644 --- a/templates/SilverStripe/Forms/TreeDropdownField.ss +++ b/templates/SilverStripe/Forms/TreeDropdownField.ss @@ -1,6 +1,6 @@