3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-08-18 11:15:42 +00:00

Added maxlength helper

This commit is contained in:
snipe
2026-07-13 22:18:25 +01:00
parent 3654149ee6
commit 44354bb072

View File

@ -1111,6 +1111,33 @@ class Helper
return false;
}
/**
* Return the numeric max length declared for a field in the model's
* validation rules (looks for `max:N`). Returns null when the model, field,
* or `max:` rule can't be found — callers should fall back to whatever
* default they want. Used by `<x-form.row>` to auto-cap text inputs to
* the DB column width without every callsite having to pass maxlength.
*/
public static function fieldMaxLength($class, string $field): ?int
{
if (! $class) {
return null;
}
$rules = $class::rules();
$rule = $rules[$field] ?? null;
if ($rule === null) {
return null;
}
$rule_string = is_array($rule) ? implode('|', $rule) : $rule;
if (preg_match('/(?:^|\|)max:(\d+)/', $rule_string, $matches)) {
return (int) $matches[1];
}
return null;
}
/**
* Check to see if the given key exists in the array, and trim excess white space before returning it
*