Skip to content

Commit 55688bb

Browse files
fix(options): protect historical keys
1 parent 0189718 commit 55688bb

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/Observers/CustomFieldObserver.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,28 @@ public function updating(Model $field): void
3737
}
3838

3939
$field->setAttribute('name', $name);
40+
$this->preventOptionRemoval($field);
4041
$this->validateOptions($field);
4142
}
4243

44+
private function preventOptionRemoval(Model $field): void
45+
{
46+
$original = array_map(
47+
static fn (array $option): string => (string) ($option['key'] ?? ''),
48+
(array) $field->getOriginal('options'),
49+
);
50+
$current = array_map(
51+
static fn (array $option): string => (string) ($option['key'] ?? ''),
52+
(array) $field->getAttribute('options'),
53+
);
54+
55+
foreach ($original as $key) {
56+
if (! in_array($key, $current, true)) {
57+
throw new InvalidArgumentException("Custom field option [{$key}] cannot be removed.");
58+
}
59+
}
60+
}
61+
4362
private function validateOptions(Model $field): void
4463
{
4564
$seen = [];

0 commit comments

Comments
 (0)