Skip to content

Commit

Permalink
[4.x] Improve array fieldtype validation for dynamically keyed fields (
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite authored Apr 8, 2024
1 parent 21ecddd commit 76230a1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
|
*/

'arr_fieldtype' => 'This is invalid.',
'code_fieldtype_rulers' => 'This is invalid.',
'date_fieldtype_date_required' => 'Date is required.',
'date_fieldtype_end_date_invalid' => 'Not a valid end date.',
Expand Down
20 changes: 20 additions & 0 deletions src/Fieldtypes/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Fieldtypes;

use Closure;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fieldtype;
use Statamic\GraphQL\Types\ArrayType;
Expand Down Expand Up @@ -81,4 +82,23 @@ public function toGqlType()
{
return GraphQL::type(ArrayType::NAME);
}

public function rules(): array
{
if ($this->isKeyed()) {
return [];
}

return [function ($handle, $value, Closure $fail) {
$values = collect($value);

if ($values->has('null')) {
$fail('statamic::validation.arr_fieldtype')->translate();
}

if ($values->count() !== $values->reject(fn ($v) => is_null($v))->count()) {
$fail('statamic::validation.arr_fieldtype')->translate();
}
}];
}
}

0 comments on commit 76230a1

Please sign in to comment.