Skip to content

fix: populate date reference dropdown when creating a custom field#172

Merged
ManukMinasyan merged 2 commits into
3.xfrom
fix/date-field-reference-options-on-create
Jun 18, 2026
Merged

fix: populate date reference dropdown when creating a custom field#172
ManukMinasyan merged 2 commits into
3.xfrom
fix/date-field-reference-options-on-create

Conversation

@ManukMinasyan

Copy link
Copy Markdown
Collaborator

Problem

When creating a new date custom field, the "After another field…" date
validation constraint shows "No options available" in its Reference field
dropdown. The same dropdown works correctly when editing an existing field.

This forces users to create the field first, save it, then re-open it in edit
mode to wire up the cross-field reference, even when sibling date fields already
exist.

Root cause

In DateConstraintField, the field_reference Select's options() closure (and
the circular-reference rule closure) resolve the entity type like this:

$entityType = $record?->entity_type ?? $get('../../../entity_type');
  • On edit, $record is the loaded CustomField, so $record->entity_type is
    used and $get is never reached → the dropdown populates.

  • On create, $record is null, so it falls back to the relative state path.
    The field-management form is rendered inside a Filament action modal, where
    form state lives under mountedActions.0.data.*. Filament's
    resolveRelativeStatePath starts from the container path mountedActions.0.data
    and strips one segment per ../:

    mountedActions.0.data → mountedActions.0 → mountedActions → (null, overshoots)
    

    The three ../ segments climb out of the form-data scope to the Livewire root,
    where no entity_type exists, so $get('../../../entity_type') returns null.
    With $entityType null the closure short-circuits to return [] → an empty list.

The existing date-validation tests didn't catch this because they inject
field_reference directly as data and configure the reference via the edit path
(where $record provides the entity type), so the create-time dropdown options
were never exercised.

Fix

Resolve entity_type and code with absolute (data-scope) paths:

$entityType = $record?->entity_type ?? $get('entity_type');
$currentCode = $record?->code ?? $get('code');

entity_type and code are always top-level keys of the field form's data, so
absolute resolution is correct in both the action-modal and page-form contexts
(and is unaffected by how deep the constraint fields are nested). Applied to both
the options() closure and the circular-reference rule closure.

Tests

Added tests/Feature/Admin/Pages/DateFieldReferenceOptionsTest.php, which mounts
the real create/edit actions and asserts the reference dropdown lists a sibling
date field. The create case fails on the previous code and passes with the fix;
the edit case is the control. Full date-validation suite (104 tests) is green.

The "After another field" date validation constraint showed "No options
available" in its reference dropdown when creating a new custom field; it
only worked when editing an existing one.

The options() and cycle-check closures resolved the entity type with the
relative state path $get('../../../entity_type'). Inside the field
management action modal the form data lives under mountedActions.0.data,
so three "../" segments climb past the form-data scope to the Livewire
root, where no entity_type exists. The closure then short-circuits to an
empty option list. On edit it only worked because the loaded $record
supplied entity_type and $get was never reached.

Resolve entity_type and code via absolute (data-scope) paths, which hold
in both the action-modal and page-form contexts. Add a regression test
covering the create and edit reference dropdowns.
@ManukMinasyan ManukMinasyan merged commit ae342ae into 3.x Jun 18, 2026
3 checks passed
@ManukMinasyan ManukMinasyan deleted the fix/date-field-reference-options-on-create branch June 18, 2026 14:34
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