fix(ui): display user-friendly message for range error (#7420)
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Has been skipped
testing / frontend-checks (push) Has been skipped
testing / test-unit (push) Has been skipped
testing / test-e2e (push) Has been skipped
testing / test-mysql (push) Has been skipped
testing / test-pgsql (push) Has been skipped
testing / test-sqlite (push) Has been skipped
testing / test-remote-cacher (redis) (push) Has been skipped
testing / test-remote-cacher (valkey) (push) Has been skipped
testing / test-remote-cacher (garnet) (push) Has been skipped
testing / test-remote-cacher (redict) (push) Has been skipped
testing / security-check (push) Has been skipped

- Instead of displaying 'RangeError: Range' display 'x must be a number between $MIN and $MAX' when the validation fails for a range error check.
- Resolves forgejo/forgejo#3510
- Added integration testing.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7420
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-04-02 15:48:09 +00:00 committed by Gusted
parent 15a2338ff2
commit 5706a2452e
4 changed files with 88 additions and 2 deletions

View file

@ -79,6 +79,11 @@ func GetInclude(field reflect.StructField) string {
return getRuleBody(field, "Include(")
}
func GetRange(field reflect.StructField) (string, string) {
min, max, _ := strings.Cut(getRuleBody(field, "Range("), ",")
return min, max
}
// Validate populates the data with validation error (if any).
func Validate(errs binding.Errors, data map[string]any, f any, l translation.Locale) binding.Errors {
if errs.Len() == 0 {
@ -131,6 +136,9 @@ func Validate(errs binding.Errors, data map[string]any, f any, l translation.Loc
data["ErrorMsg"] = trName + l.TrString("form.url_error", errs[0].Message)
case binding.ERR_INCLUDE:
data["ErrorMsg"] = trName + l.TrString("form.include_error", GetInclude(field))
case binding.ERR_RANGE:
min, max := GetRange(field)
data["ErrorMsg"] = trName + l.TrString("alert.range_error", l.PrettyNumber(min), l.PrettyNumber(max))
case validation.ErrGlobPattern:
data["ErrorMsg"] = trName + l.TrString("form.glob_pattern_error", errs[0].Message)
case validation.ErrRegexPattern: