Skip to content

Commit

Permalink
Merge pull request #83 from systopia/fix-radio-with-boolean
Browse files Browse the repository at this point in the history
Fix radios input with booleans
  • Loading branch information
dontub authored Jan 30, 2025
2 parents 521da17 + fd48632 commit 8308512
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/Form/Control/Callbacks/OptionValueCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ public static function validate(array $element, FormStateInterface $formState):
* @return mixed
*/
public static function value(array $element, $input, FormStateInterface $formState) {
$defaultUsed = FALSE;
if (FALSE === $input) {
$input = $element['#default_value'] ?? NULL;
$defaultUsed = TRUE;
}

if (NULL === $input) {
Expand All @@ -64,28 +66,11 @@ public static function value(array $element, $input, FormStateInterface $formSta
return NULL;
}

if (in_array($input, $element['#_option_values'], TRUE)
|| self::isIntegerish($input) && in_array((int) $input, $element['#_option_values'], TRUE)
) {
if (!$defaultUsed && is_string($input) && array_key_exists($input, $element['#_option_values'])) {
$input = $element['#_option_values'][$input];
}

return $input;
}

/**
* @param mixed $value
*
* @return bool
*
* @phpstan-assert-if-true float|string|int $value
*/
private static function isIntegerish($value): bool {
if (\is_string($value)) {
$value = \trim($value);
}

return \is_numeric($value) && $value == (string) $value;
}

}

0 comments on commit 8308512

Please sign in to comment.