Skip to content

Commit b5c921f

Browse files
committed
Move the client validation rule payload into data- attribute of the custom element
1 parent d706ba9 commit b5c921f

4 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/Components/Web.JS/src/Validation/Adapters/BlazorAdapter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { ValidatableElement } from '../ValidationTypes';
77

88
export const ClientValidationElementName = 'blazor-client-validation-data';
99

10+
// The attribute on the carrier element that holds the serialized validation rules. The payload
11+
// lives in an attribute (not text content) so the carrier element renders nothing - no CSS or
12+
// hidden attribute is needed to keep it out of the visible page.
13+
const ClientValidationRulesAttributeName = 'data-rules';
14+
1015
interface ClientValidationFormDescriptor {
1116
fields: ClientValidationFieldDescriptor[];
1217
}
@@ -78,7 +83,7 @@ export function defineBlazorClientValidationDataElement(
7883
form.setAttribute('novalidate', '');
7984
}
8085

81-
const payload = this.textContent || '';
86+
const payload = this.getAttribute(ClientValidationRulesAttributeName) || '';
8287
if (this.appliedPayload === payload) {
8388
// Rules unchanged - leave the existing registration and live error display intact.
8489
return;

src/Components/Web.JS/test/Validation/Adapters/BlazorAdapter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('blazor-client-validation-data custom element', () => {
161161
form.appendChild(input);
162162

163163
const carrier = document.createElement(ClientValidationElementName);
164-
carrier.textContent = payload;
164+
carrier.setAttribute('data-rules', payload);
165165
form.appendChild(carrier);
166166

167167
document.body.appendChild(form);
@@ -196,7 +196,7 @@ describe('blazor-client-validation-data custom element', () => {
196196
// Simulate the morph: same input element reused but renamed, novalidate stripped, payload changed.
197197
input.name = 'Beta';
198198
form.removeAttribute('novalidate');
199-
carrier.textContent = fieldPayload('Beta', [{ name: 'required', message: 'Beta required.' }]);
199+
carrier.setAttribute('data-rules', fieldPayload('Beta', [{ name: 'required', message: 'Beta required.' }]));
200200

201201
reconcileValidationElements();
202202

src/Components/Web/src/Forms/ClientValidation/ClientValidationData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public Task SetParametersAsync(ParameterView parameters)
8080
_handle.Render(builder =>
8181
{
8282
builder.OpenElement(0, "blazor-client-validation-data");
83-
builder.AddMarkupContent(1, json);
83+
// The rules are carried in the data-rules attribute, not as element content, so the
84+
// element renders nothing at all in the DOM.
85+
builder.AddAttribute(1, "data-rules", json);
8486
builder.CloseElement();
8587
});
8688
return Task.CompletedTask;

src/Components/test/E2ETest/ServerRenderingTests/ClientValidation/ClientValidationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void CarrierElement_IsRenderedInsideForm_WithExpectedJsonShape()
8080
Assert.Single(Browser.FindElements(By.CssSelector("form blazor-client-validation-data")));
8181

8282
var json = (string)((IJavaScriptExecutor)Browser).ExecuteScript(
83-
"return document.querySelector('blazor-client-validation-data').textContent;");
83+
"return document.querySelector('blazor-client-validation-data').getAttribute('data-rules');");
8484

8585
using var document = JsonDocument.Parse(json);
8686
var fields = document.RootElement.GetProperty("fields").EnumerateArray().ToList();

0 commit comments

Comments
 (0)