Add validation on max length

This commit is contained in:
Pieter Vander Vennet 2024-07-16 12:49:17 +02:00
parent 7b47cff62e
commit 670b40beb8
2 changed files with 21 additions and 4 deletions

View file

@ -1,7 +1,24 @@
import { Validator } from "../Validator" import { Validator } from "../Validator"
import { Translation } from "../../i18n/Translation"
import Translations from "../../i18n/Translations"
export default class StringValidator extends Validator { export default class StringValidator extends Validator {
constructor() {
super("string", "A simple piece of text") constructor(type?: string, doc?: string, inputmode?: "none" | "text" | "tel" | "url" | "email" | "numeric" | "decimal" | "search", textArea?: boolean) {
super(type ?? "string",
doc ?? "A simple piece of text which is at most 255 characters long",
inputmode,
textArea)
}
isValid(s: string): boolean {
return s.length <= 255
}
getFeedback(s: string, getCountry?: () => string): Translation | undefined {
if (s.length > 255) {
return Translations.t.validation.tooLong.Subs({ count: s.length })
}
return super.getFeedback(s, getCountry)
} }
} }

View file

@ -1,6 +1,6 @@
import { Validator } from "../Validator" import StringValidator from "./StringValidator"
export default class TextValidator extends Validator { export default class TextValidator extends StringValidator {
constructor() { constructor() {
super( super(
"text", "text",