Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Ensure schema data includes attributes #11469

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
*/
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
25 changes: 18 additions & 7 deletions tests/php/Forms/FormSchemaTest/testGetNestedSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "text"
},
"data": {
"maxlength": null
},
Expand All @@ -53,7 +55,9 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "hidden"
},
"data": [],
"validation": []
}
Expand All @@ -77,7 +81,8 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "submit"
"type": "submit",
"class": "action"
},
"data": {
"icon": "save"
Expand All @@ -102,7 +107,8 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "button"
"type": "button",
"class": "action"
},
"data": {
"icon": null
Expand All @@ -126,7 +132,10 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "field CompositeField popover",
"tabindex": null
},
"data": {
"popoverTitle": null,
"placement": "bottom",
Expand Down Expand Up @@ -159,7 +168,8 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "submit"
"type": "submit",
"class": "action"
},
"data": {
"icon": null
Expand All @@ -184,7 +194,8 @@
"autoFocus": false,
"customValidationMessage": "",
"attributes": {
"type": "submit"
"type": "submit",
"class": "action"
},
"data": {
"icon": null
Expand Down
4 changes: 3 additions & 1 deletion tests/php/Forms/FormSchemaTest/testGetSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"disabled": false,
"autoFocus": false,
"customValidationMessage": "",
"attributes": [],
"attributes": {
"class": "hidden"
},
"data": [],
"validation": []
}
Expand Down
27 changes: 22 additions & 5 deletions tests/php/Forms/FormSchemaTest/testSchemaValidation.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
"length": 40
}
},
"attributes": [],
"attributes": {
"class": "text",
"required": "required",
"aria-required": "true",
"maxLength": 40,
"size": 30
},
"data": {
"maxlength": 40
}
Expand All @@ -62,7 +68,12 @@
"validation": {
"date": true
},
"attributes": [],
"attributes": {
"class": "date text",
"lang": "en-US",
"min": null,
"max": null
},
"data": {
"html5": true,
"min": null,
Expand Down Expand Up @@ -90,7 +101,9 @@
"validation": {
"numeric": true
},
"attributes": [],
"attributes": {
"class": "numeric text"
},
"data": {
"maxlength": null
}
Expand All @@ -115,7 +128,9 @@
"validation": {
"currency": true
},
"attributes": [],
"attributes": {
"class": "currency text"
},
"data": {
"maxlength": null
}
Expand All @@ -138,7 +153,9 @@
"autoFocus": false,
"customValidationMessage": "",
"validation": [],
"attributes": [],
"attributes": {
"class": "hidden"
},
"data": []
}
],
Expand Down