RJSF editor broken on new entity forms: NULL default value, TypeError in setPropertyByPath, and null values fail server validation
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3584167. -->
Reported by: [benstallings](https://www.drupal.org/user/210009)
Related to !11
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p> The RJSF editor widget is unusable on entity add forms (new entities with no existing field value). Three related bugs prevent form submission:</p>
<p> 1. The widget passes NULL as the default value, which serializes to "null" in the hidden input<br>
2. setPropertyByPath() throws a TypeError when traversing null array items<br>
3. Server-side validation rejects the form because RJSF sets unset optional fields to null, which fails JSON Schema type checks</p>
<p> Additionally, opis/json-schema 2.1.0 triggers PHP deprecation notices on PHP 8.1+ due to incompatible Iterator return types in ErrorContainer.</p>
<p> Bug 1: NULL default value</p>
<p> In RjsfEditorWidget::formElement(), when no field value exists:</p>
<p><code> '#default_value' => isset($items[$delta]->value) ? json_decode($items[$delta]->value) : NULL,</code></p>
<p> NULL is passed to RjsfEditor::processElement(), which encodes it as Json::encode(NULL) → "null" in the hidden input. RJSF receives JavaScript null as formData, causing it to generate malformed defaults with null array items (e.g., {"status_map": [null]}).</p>
<p> Bug 2: TypeError in setPropertyByPath</p>
<p> When RJSF fires onChange and the saveFormData callback traverses into a null array item:</p>
<pre> const setPropertyByPath = (obj, value, path) => {<br> for (i = 0; i < path.length - 1; i++) {<br> obj = obj[path[i]]; // obj becomes null when traversing into null array items<br> }<br> obj[path[i]] = value; // TypeError: Cannot set properties of null<br> };</pre><p> This prevents the form from being submitted via the normal UI flow.</p>
<p> Bug 3: Null values fail server-side validation</p>
<p> RJSF's JavaScript sets unset optional fields to null in the form data (e.g., "uid": null). When the PHP-side validateInput() passes this to opis/json-schema, validation fails because null is not a valid value for "type": "number" unless the schema explicitly allows it with "type": ["number", "null"]. This is a mismatch between RJSF's JS behavior and the server-side validator's strict interpretation.</p>
<p> Bug 4: Opis deprecation notices on PHP 8.1+</p>
<p> opis/json-schema 2.1.0 (pinned by the module's composer.json) has Iterator method signatures (next(), rewind()) with return types incompatible with PHP 8.1+. The ErrorContainer class triggers E_DEPRECATED notices on autoload during validation, which surface as error messages in Drupal.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<p> 1. Create a field of type rjsf_data on any entity type, with a schema containing an array of objects (e.g., "items": {"type": "object"})<br>
2. Navigate to the entity add form<br>
3. Fill in required fields and click Save<br>
4. Result: Form either does not submit (JS error) or returns a server validation error</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p> Bug 1 — RjsfEditorWidget.php: Return new \stdClass() instead of NULL when no field value exists. This produces "{}" in the hidden input, giving RJSF a valid empty object to work with.</p>
<p> Bug 2 — editor.tsx: Add null/undefined guards in setPropertyByPath(). When an intermediate value is null during traversal, initialize it as {} (if the next path segment is a property name) or [] (if numeric):</p>
<pre> if (obj[path[i]] === null || obj[path[i]] === undefined) {<br> obj[path[i]] = isNaN(Number(path[i + 1])) ? {} : [];<br> }</pre><p> Bug 3 — RjsfEditor.php: Strip null values from the decoded JSON before passing to the Opis validator. Also skip validation entirely for empty objects ({}), since there is nothing to validate. Null properties represent "not set" in RJSF and should be treated as absent rather than as invalid typed values.</p>
<p> Bug 4 — RjsfEditor.php: Temporarily suppress E_DEPRECATED during the Opis validation call. This is a workaround until opis/json-schema is upgraded to a PHP 8.1+-compatible version or replaced.</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<p>make a MR</p>
issue
GitLab AI Context
Project: project/rjsf
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/rjsf/-/raw/1.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/rjsf
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