Make client-side JSON Editor validation toggle-aware for editorial workflows
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3594479. -->
Reported by: [slucero](https://www.drupal.org/user/1612534)
Related to !195
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>Patternkit introduces an admin-configurable enforcement toggle (<code>render_validation_on_save</code>, default TRUE) in <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/patternkit/-/work_items/3589846" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/patternkit/-/work_items/3589846</a></span> that blocks saving a PatternkitBlock when its content fails server-side JSON Schema validation. The toggle is designed as a safety valve for sites that accumulated legacy-invalid content before validation was introduced: an operator can disable enforcement temporarily, let editors re-save blocks through the browser UI, then re-enable enforcement once remediation is complete.</p>
<p>The toggle is ineffective for its primary intended use case. When an editor opens a block in Layout Builder, the JSON Editor library validates the stored content immediately and blocks form submission if errors are found. This happens entirely client-side, before the form reaches the server. The server-side toggle never applies. Editors are stuck: they cannot save any change (including unrelated edits such as updating body text) without fixing every validation error in the block, including fields they may not know how to correct.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<ol>
<li>Install Patternkit with a pattern that has a required field.</li>
<li>Save a PatternkitBlock with invalid content via Drush <code>pkset</code> or the entity API to simulate a legacy-invalid state.</li>
<li>Go to <strong>Administration > Configuration > Patternkit</strong> and disable <strong>Validate on save</strong> (<code>render_validation_on_save = FALSE</code>).</li>
<li>Open the affected block in Layout Builder.</li>
<li>Make an unrelated change (e.g., update a body text field).</li>
<li>Click <strong>Update</strong>.</li>
</ol>
<p><strong>Observed:</strong> JSON Editor blocks submission. The form does not reach the server. The toggle has no effect.</p>
<p><strong>Expected:</strong> With the toggle off, the editor can save the block. Validation issues are displayed as non-blocking warnings so the editor is informed but not stopped.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Make the client-side validation gates in <code>patternkit.jsoneditor.js</code> toggle-aware. When the toggle is off, submission with errors is intercepted once to show non-blocking warnings; a second click proceeds. When the toggle is on (the default), behavior is unchanged.</p>
<h4>Config delivery</h4>
<p><code>JsonEditorFormBuilder::buildEditorForm()</code> adds <code>renderValidationOnSave</code> to <code>drupalSettings.patternkitEditor</code> via the existing settings pipeline. <code>JsonEditorForm::defaultEditorSettings()</code> injects the value into <code>#attached</code>.</p>
<h4>Submit-boundary intercept (toggle off)</h4>
<p>On first submit with errors, <code>validateBeforeAjaxSubmit()</code> or <code>onSubmit()</code> intercepts and:</p>
<ul>
<li>Inserts a <code>messages--warning</code> block above the submit button</li>
<li>Relabels the button from <strong>Update</strong> to <strong>Save with issues</strong></li>
<li>Adds a CSS class to the editor container to shift inline indicators from red to amber</li>
</ul>
<p>A second click serializes editor state via <code>saveSchema()</code> and submits. Warning state persists until all issues are resolved or the form closes; resolution fully reverts the button and message.</p>
<h4>Custom validator</h4>
<p><code>patternkitRequiredStringValidator</code> continues to run unconditionally. The gate functions, not the validator, decide whether to block.</p>
<h4>Unchanged behavior (toggle on)</h4>
<p>No change to current behavior.</p>
<ul>
<li>Validation errors block submission.</li>
<li>The warning message, button relabel, and amber indicators are never shown.</li>
</ul>
<h4>Status report warning</h4>
<p><code>hook_requirements</code> in <code>patternkit.install</code> emits a <code>REQUIREMENT_WARNING</code> on <code>admin/reports/status</code> while <code>render_validation_on_save</code> is FALSE.</p>
<h4>Tests</h4>
<p><code>ValidationToggleTest</code> covers five FunctionalJavascript scenarios: toggle-off submit proceeds; toggle-on blocks; toggle-off with valid content saves normally; editor fixes issues and warning clears; warning re-triggers after a fresh validation failure.</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ul>
<li>Review and test the merge request.</li>
<li>CI validation.</li>
</ul>
<h3 id="summary-ui-changes">User interface changes</h3>
<ul>
<li><strong>Warning message block:</strong> A <code>messages--warning</code> message block appears above the submit button when the toggle is off and a submit attempt finds validation errors. The message is removed when all issues are resolved.</li>
<li><strong>Button relabel:</strong> The submit button changes from <strong>Update</strong> to <strong>Update with issues</strong> in the warning state and reverts when all issues are resolved.</li>
<li><strong>Status report warning:</strong> A new warning entry appears on <code>admin/reports/status</code> when <code>render_validation_on_save</code> is FALSE.</li>
</ul>
<h3 id="summary-introduced-terminology">Introduced terminology</h3>
<p>None.</p>
<h3 id="summary-api-changes">API changes</h3>
<p><code>JsonEditorFormBuilder::buildEditorForm()</code> adds a <code>renderValidationOnSave</code> boolean to the <code>drupalSettings.patternkitEditor</code> object. This is a new key in an existing settings namespace; no existing keys are changed or removed.</p>
<h3 id="summary-data-model-changes">Data model changes</h3>
<p>None.</p>
<h3 id="summary-release-notes">Release notes snippet</h3>
<p>Patternkit now makes the client-side JSON Editor validation gate toggle-aware. When the save-time enforcement toggle is disabled (<code>render_validation_on_save = FALSE</code>), editors can save blocks with validation issues through the Layout Builder UI. Validation errors are displayed as non-blocking warnings: a message block appears above the submit button and the button relabels to <strong>Save/Update with issues</strong>, so the editor is informed but not stopped. When the toggle is on (the default), behavior is unchanged and validation errors continue to block submission. A warning also appears on the site status report while enforcement is disabled, reminding administrators that invalid content can be saved until re-enabled.</p>
<h4>Known issues</h4>
<ul>
<li><strong>Stale toggle value:</strong> The toggle value is delivered to JavaScript at form render time via <code>drupalSettings</code>. If an administrator changes the toggle while an editor has a block form open, the open form uses the stale value until the page is refreshed. This is inherent to how <code>drupalSettings</code> works and affects only the narrow window between a settings change and a page reload.</li>
</ul>
issue
GitLab AI Context
Project: project/patternkit
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/patternkit/-/raw/9.1.x/CONTRIBUTING.md — contribution guidelines
- https://git.drupalcode.org/project/patternkit/-/raw/9.1.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/patternkit
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD