fix: populate date reference dropdown when creating a custom field#172
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, thefield_referenceSelect'soptions()closure (andthe circular-reference rule closure) resolve the entity type like this:
On edit,
$recordis the loadedCustomField, so$record->entity_typeisused and
$getis never reached → the dropdown populates.On create,
$recordisnull, 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'sresolveRelativeStatePathstarts from the container pathmountedActions.0.dataand strips one segment per
../:The three
../segments climb out of the form-data scope to the Livewire root,where no
entity_typeexists, so$get('../../../entity_type')returnsnull.With
$entityTypenull the closure short-circuits toreturn []→ an empty list.The existing date-validation tests didn't catch this because they inject
field_referencedirectly as data and configure the reference via the edit path(where
$recordprovides the entity type), so the create-time dropdown optionswere never exercised.
Fix
Resolve
entity_typeandcodewith absolute (data-scope) paths:entity_typeandcodeare always top-level keys of the field form's data, soabsolute 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 mountsthe 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.