Skip to content

Commit d4901d5

Browse files
committed
fix(nicknames): handle non-int min/max length settings
Cast the persisted setting to int before handing it to Schema\Str's minLength/maxLength, and only apply the constraint when the admin has configured a positive value. An empty field saved from the admin panel was otherwise stored as '' — triggering a TypeError in PHP's default weak coercion — and, after the naive cast, coerced to 0, which made maxLength(0) reject every nickname. Reveals a gap in Schema\Str: the int-only signature offers no way to "disable" a length constraint via the condition argument without a guard at the call site. Keeping the guard local for this RC fix.
1 parent 42362b6 commit d4901d5

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

extensions/nicknames/src/Api/UserResourceFields.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public function __invoke(): array
3232
$regex = "/$regex/";
3333
}
3434

35+
// Settings are always returned as strings from the DB. Coerce and only
36+
// apply the length constraint when the admin has configured a non-zero
37+
// value; an empty field saved from the admin panel would otherwise be
38+
// stored as '' and cast to 0, making maxLength(0) reject every value.
39+
$min = (int) $this->settings->get('flarum-nicknames.min');
40+
$max = (int) $this->settings->get('flarum-nicknames.max');
41+
3542
return [
3643
Schema\Str::make('nickname')
3744
->visible(false)
@@ -43,8 +50,8 @@ public function __invoke(): array
4350
// may render as hyperlinks in notification emails.
4451
->rule('not_regex:/[\[\]()<>]/')
4552
->regex($regex ?? '', ! empty($regex))
46-
->minLength($this->settings->get('flarum-nicknames.min'))
47-
->maxLength($this->settings->get('flarum-nicknames.max'))
53+
->minLength($min, $min > 0)
54+
->maxLength($max, $max > 0)
4855
->unique('users', 'nickname', true, (bool) $this->settings->get('flarum-nicknames.unique'))
4956
->unique('users', 'username', true, (bool) $this->settings->get('flarum-nicknames.unique'))
5057
->validationMessages([

0 commit comments

Comments
 (0)