Skip to content

Commit

Permalink
Properly validate select fields
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Feb 28, 2025
1 parent aa3e9c6 commit d9f0c1f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions formwork/fields/select.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Formwork\Cms\App;
use Formwork\Fields\Exceptions\ValidationException;
use Formwork\Fields\Field;
use Formwork\Utils\Arr;
use Formwork\Utils\Constraint;

return function (App $app) {
return [
Expand All @@ -11,6 +13,14 @@
},

'validate' => function (Field $field, $value) {
if (Constraint::isEmpty($value)) {
return '';
}

if (!array_key_exists($value, $field->options())) {
throw new ValidationException(sprintf('Invalid value for field "%s" of type "%s"', $field->name(), $field->type()));
}

if (is_numeric($value)) {
// This reliably casts numeric values to int or float
return $value + 0;
Expand Down

0 comments on commit d9f0c1f

Please sign in to comment.