Skip to content

Commit c856a28

Browse files
author
Dominic Tubach
committed
Ensure value callbacks get correct input on array item remove
1 parent 3368b5f commit c856a28

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Form/Control/Callbacks/ArrayCallbacks.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static function addItem(array &$form, FormStateInterface $formState): voi
5454
$data = $formObject->calculateData($formState);
5555
$formState->setTemporary($data);
5656

57+
$formState->clearErrors();
5758
$formState->setRebuild();
5859
}
5960

@@ -145,6 +146,14 @@ public static function removeItem(array &$form, FormStateInterface $formState):
145146
Assertion::integer($pos);
146147
$indexToRemove = (int) substr($name, $pos + 1);
147148

149+
$formInput = $formState->getUserInput();
150+
$arrayInput = &NestedArray::getValue($formInput, $propertyPath);
151+
if (is_array($arrayInput)) {
152+
// Update input so value callbacks get the correct values.
153+
unset($arrayInput[$indexToRemove]);
154+
$arrayInput = array_values($arrayInput);
155+
}
156+
148157
$arrayItems = $formState->getValue($propertyPath);
149158
Assertion::isArray($arrayItems);
150159
unset($arrayItems[$indexToRemove]);
@@ -163,6 +172,7 @@ public static function removeItem(array &$form, FormStateInterface $formState):
163172
Assertion::integer($numItems);
164173
$propertyAccessor->setProperty('numItems', $numItems - 1);
165174

175+
$formState->clearErrors();
166176
$formState->setRebuild();
167177
}
168178

0 commit comments

Comments
 (0)