Skip to content

A free open source IT asset/license management system

Added maxlength helper

snipe44354bb07209
diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php
index d96d72721e..e0dab95e28 100644
--- a/app/Helpers/Helper.php
+++ b/app/Helpers/Helper.php
@@ -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
*