Skip to content

fix: prevent self-clear of field_code in visibility condition repeater#146

Open
erikpach wants to merge 1 commit intorelaticle:3.xfrom
erikpach:fix/visibility-field-self-clear
Open

fix: prevent self-clear of field_code in visibility condition repeater#146
erikpach wants to merge 1 commit intorelaticle:3.xfrom
erikpach:fix/visibility-field-self-clear

Conversation

@erikpach
Copy link
Copy Markdown

@erikpach erikpach commented May 6, 2026

Summary

Fixes a bug in the visibility conditions UI for custom fields where selecting a value in the Field (field_code) Select inside the conditions Repeater immediately clears the selection.

Reproduction

  1. Open Custom Fields management page.
  2. Edit any field (or section).
  3. Set Visibility mode to anything that requires conditions (e.g. Visible when).
  4. Inside the conditions Repeater, pick a value in the Field select.
  5. The select clears itself instantly. The user cannot save a working condition.

The bug also affects the ConditionSource::ModelAttribute source where the dropdown of model attributes appears, gets selected, and disappears.

Root cause

src/Filament/Management/Forms/Components/VisibilityComponent.php line 127:

$schema[] = Select::make('field_code')
    ...
    ->afterStateUpdated(fn (Get $get, Set $set) => $this->resetConditionValues($get, $set))

resetConditionValues() is shared with the source Select and contains:

private function resetConditionValues(?Get $get, Set $set): void
{
    $this->clearAllValueFields($set);
    $set('field_code', null); // wipes the user's freshly selected value
    ...
}

So when the user picks field_code, Filament fires afterStateUpdated, which sets field_code back to null.

Fix

Split the reset path:

  • resetConditionValues() keeps its current behavior and remains the callback for the source Select (it should clear field_code when the source switches).
  • New resetValuesAndOperator() is used by the field_code Select. It only clears the value fields and resets the operator to the first compatible option. It does not touch field_code.

Verification

  • Existing test suite passes (654 passed).
  • Manual reproduction confirmed before / after.

Selecting a value in the 'field_code' Select inside the visibility
conditions Repeater triggered an afterStateUpdated callback that called
resetConditionValues(), which itself sets field_code to null. The just
selected value was wiped immediately, so users could not configure
conditions.

Split the reset into two operations:
- resetConditionValues() (used by source change) clears field_code,
  values, and operator.
- resetValuesAndOperator() (used by field_code change) only clears the
  value fields and updates the default operator, preserving the user
  selected field_code.
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