From dfc1b5471353192a9b7706662999873c64ffb4b2 Mon Sep 17 00:00:00 2001 From: Hippolyte Date: Mon, 18 Feb 2019 13:48:40 +0100 Subject: [PATCH] Add NotEmpty validator for string fields --- declarative/validators.go | 7 +++++++ validators.go | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/declarative/validators.go b/declarative/validators.go index 7f6e8e66..89f2c0ab 100644 --- a/declarative/validators.go +++ b/declarative/validators.go @@ -42,6 +42,13 @@ func (SelRequired) Create() (walk.Validator, error) { return walk.SelectionRequiredValidator(), nil } +type NotEmpty struct { +} + +func (NotEmpty) Create() (walk.Validator, error) { + return walk.NotEmptyValidator(), nil +} + type dMultiValidator struct { validators []Validator } diff --git a/validators.go b/validators.go index 2f256f1c..65fc1260 100644 --- a/validators.go +++ b/validators.go @@ -150,3 +150,21 @@ func (selectionRequiredValidator) Validate(v interface{}) error { return nil } + +type notEmptyValidator struct { +} + +var notEmptyValidatorSingleton Validator = notEmptyValidator{} + +func NotEmptyValidator() Validator { + return notEmptyValidatorSingleton +} + +func (notEmptyValidator) Validate(v interface{}) error { + if s := v.(string); len(s) <= 0 { + return NewValidationError( + tr("Empty field", "walk"), + tr("Please fill out the required fields.", "walk")) + } + return nil +}