Skip to content

Commit

Permalink
FIX Ensure schema data includes attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 19, 2024
1 parent eb8907f commit 9eafc8b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17 deletions.
20 changes: 20 additions & 0 deletions src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,14 @@ 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) {
unset($attributes[$key]);
}
$data['attributes'] = $attributes;
return $data;
}

Expand Down Expand Up @@ -1562,6 +1570,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->getSchemaData())) . '"';
return DBHTMLText::create()->setValue($content);
}

/**
* @return Tip
*/
Expand Down
1 change: 0 additions & 1 deletion src/Forms/SearchableDropdownTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ public function getAttributes(): array
parent::getAttributes(),
[
'name' => $name,
'data-schema' => json_encode($this->getSchemaData()),
]
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Forms/TreeDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion templates/SilverStripe/Forms/TreeDropdownField.ss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div
class="TreeDropdownField<% if $extraClass %> $extraClass<% end_if %><% if $ShowSearch %> searchable<% end_if %>"
$AttributesHTML('class')
$AttributesHTML('class') $SchemaAttributesHtml
<% if $Metadata %>data-metadata="$Metadata.ATT"<% end_if %>
>
<input id="$ID" type="hidden" name="$Name.ATT" value="$Value.ATT" />
Expand Down
10 changes: 10 additions & 0 deletions tests/php/Forms/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ public function testGetSchemaDataDefaultsTitleTip()
$this->assertSame('Test tip', $schema['titleTip']['content']);
}

public function testGetSchemaDataDefaultsAttributes()
{
$field = new FormField('MyField');
$field->setAttribute('foo', 'bar');
$this->assertEquals('bar', $field->getAttribute('foo'));
$schema = $field->getSchemaDataDefaults();
$this->assertArrayHasKey('foo', $schema['attributes']);
$this->assertEquals('bar', $schema['attributes']['foo']);
}

public function testGetSchemaData()
{
$field = new FormField('MyField');
Expand Down
39 changes: 32 additions & 7 deletions tests/php/Forms/FormSchemaTest/testGetNestedSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "text",
"readonly": false,
"autofocus": false
},
"data": {
"maxlength": null
},
Expand All @@ -53,7 +57,11 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "hidden",
"readonly": false,
"autofocus": false
},
"data": [],
"validation": []
}
Expand All @@ -77,7 +85,10 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "submit"
"type": "submit",
"class": "action",
"readonly": false,
"autofocus": false
},
"data": {
"icon": "save"
Expand All @@ -102,7 +113,10 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "button"
"type": "button",
"class": "action",
"readonly": false,
"autofocus": false
},
"data": {
"icon": null
Expand All @@ -126,7 +140,12 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "field CompositeField popover",
"readonly": null,
"autofocus": false,
"tabindex": null
},
"data": {
"popoverTitle": null,
"placement": "bottom",
Expand Down Expand Up @@ -159,7 +178,10 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "submit"
"type": "submit",
"class": "action",
"readonly": false,
"autofocus": false
},
"data": {
"icon": null
Expand All @@ -184,7 +206,10 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "submit"
"type": "submit",
"class": "action",
"readonly": false,
"autofocus": false
},
"data": {
"icon": null
Expand Down
6 changes: 5 additions & 1 deletion tests/php/Forms/FormSchemaTest/testGetSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "hidden",
"readonly": false,
"autofocus": false
},
"data": [],
"validation": []
}
Expand Down
37 changes: 32 additions & 5 deletions tests/php/Forms/FormSchemaTest/testSchemaValidation.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@
"length": 40
}
},
"attributes": [],
"attributes": {
"class": "text",
"readonly": false,
"autofocus": false,
"required": "required",
"aria-required": "true",
"maxLength": 40,
"size": 30
},
"data": {
"maxlength": 40
}
Expand All @@ -62,7 +70,14 @@
"validation": {
"date": true
},
"attributes": [],
"attributes": {
"class": "date text",
"readonly": false,
"autofocus": false,
"lang": "en-US",
"min": null,
"max": null
},
"data": {
"html5": true,
"min": null,
Expand Down Expand Up @@ -90,7 +105,11 @@
"validation": {
"numeric": true
},
"attributes": [],
"attributes": {
"class": "numeric text",
"readonly": false,
"autofocus": false
},
"data": {
"maxlength": null
}
Expand All @@ -115,7 +134,11 @@
"validation": {
"currency": true
},
"attributes": [],
"attributes": {
"class": "currency text",
"readonly": false,
"autofocus": false
},
"data": {
"maxlength": null
}
Expand All @@ -138,7 +161,11 @@
"autoFocus": false,
"customValidationMessage": "",
"validation": [],
"attributes": [],
"attributes": {
"class": "hidden",
"readonly": false,
"autofocus": false
},
"data": []
}
],
Expand Down

0 comments on commit 9eafc8b

Please sign in to comment.