Skip to content

Evaluate showIf/requiredIf conditions on locked fields when rendering - #400

Open
adrianbj wants to merge 1 commit into
processwire:devfrom
adrianbj:locked-field-dependencies
Open

adrianbj wants to merge 1 commit into
processwire:devfrom
adrianbj:locked-field-dependencies

Conversation

@adrianbj

@adrianbj adrianbj commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Refs processwire/processwire-issues#1079

A locked or non-editable Inputfield renders its value rather than an input, so inputfields.js has nothing to read for a showIf/requiredIf condition on it: the dependent field stays hidden (or its requiredIf never applies) whatever the locked field's value, while processInput() evaluates the same condition correctly.

This takes a different approach from the hidden-value workarounds in the issue, to address the concerns raised there: no value is rendered into the page. Since a locked field's value can't change in the browser, a condition on it has a fixed result when the form renders, so it is evaluated there and only what's left goes to the browser.

InputfieldForm::resolveLockedDependencies($selectorString) evaluates the conditions on locked fields with selectorMatchesInputfield(), the code processInput() already uses, so .count, multi-value fields, and option labels behave exactly as they do in processing. It returns [$matches, $remainingSelector]:

Condition Rendered
on locked fields, matches removed from data-show-if / data-required-if
showIf on locked fields, can't match field rendered already hidden (InputfieldStateHidden, display:none), the state inputfields.js leaves it in, so fields that depend on it can still read it
requiredIf on locked fields, can't match field rendered as not required
naming locked and editable fields (a|b=1) reduced to the editable fields, unless a locked one matches
OR-groups, fields not in the form, locked fields with a showIf of their own unchanged

InputfieldWrapper::render() applies this to local variables only, so the Inputfield's showIf, requiredIf, and required settings are untouched and processing evaluates the original conditions. The only thing the page reveals is whether a field shows, which it shows anyway.

"Locked" means rendered as a value: one of the locked collapsed states (the same list render() uses), or editable() false, which covers field-level access control (the case from the issue where a user can view but not edit the dependency field).

InputfieldForm::render() records itself as the form being rendered (InputfieldWrapper::$renderForms), because preRenderChildren() moves children into wrappers that have no parent, so getForm() returns null from the render loop.

Testing

  • New testLockedDependencies() in InputfieldForm.test.php (23 checks): matching, non-matching, and mixed conditions; a|b across locked and editable fields; option labels; checkboxes with =, !=, and .count; a view-only (editable(false)) dependency; a field in a nested fieldset; requiredIf true/false/mixed; OR-groups and locked fields with their own showIf left unchanged; settings unchanged after render; the locked value not copied into the markup. The existing InputfieldForm tests still pass.
  • On a site whose user template has a locked user_type (and a locked support_need for some users) with about a dozen fields depending on them, the rendered user edit forms match a site-level hook version of this, field for field.

Not covered: Inputfields inside repeater items rendered in value mode, which Robin (Toutouwai) noted don't get data-show-if at all; this leaves them as they are.

A locked or non-editable Inputfield renders its value rather than an input,
so inputfields.js has nothing to read for a showIf or requiredIf condition
on it, and the dependent field is hidden (or its requiredIf never applies)
whatever the locked field's value. Processing evaluates these conditions
correctly, so the form shows one thing and processes another.

A locked field's value can't change in the browser, so a condition on it has
a fixed result when the form is rendered. InputfieldForm::resolveLockedDependencies()
evaluates it with selectorMatchesInputfield(), the same code processInput()
uses, and InputfieldWrapper::render() renders what is left:

- a condition on locked fields that matches is removed from data-show-if or
  data-required-if
- a showIf that can't match renders the field already hidden, as
  inputfields.js hides it (still in the page, so fields that depend on it can
  read it)
- a requiredIf that can't match renders the field as not required
- a condition naming locked and editable fields (a|b=1) keeps only the
  editable ones, unless a locked one matches

No value is copied into the page, only whether a field is shown, and
multi-value fields, .count, and option labels behave as they do in
processing. OR-groups, fields outside the form, and locked fields with a
showIf of their own are left unchanged. The Inputfields' own settings are
not modified, so processing evaluates the original conditions.

InputfieldForm::render() records itself as the form being rendered, since
preRenderChildren() moves children into wrappers without a parent, so
getForm() can't find it from the render loop.

Fixes processwire/processwire-issues#1079
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant