42 lines
817 B
Go
42 lines
817 B
Go
|
/*
|
||
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||
|
*/
|
||
|
|
||
|
package form
|
||
|
|
||
|
import (
|
||
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
||
|
"errors"
|
||
|
"net/mail"
|
||
|
)
|
||
|
|
||
|
type Validator struct {
|
||
|
l *locale.Locale
|
||
|
AllOK bool
|
||
|
}
|
||
|
|
||
|
func NewValidator(l *locale.Locale) *Validator {
|
||
|
return &Validator{
|
||
|
l: l,
|
||
|
AllOK: true,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (v *Validator) CheckRequired(input *Input, message string) bool {
|
||
|
return v.check(input, input.Val != "", message)
|
||
|
}
|
||
|
|
||
|
func (v *Validator) CheckValidEmail(input *Input, message string) bool {
|
||
|
_, err := mail.ParseAddress(input.Val)
|
||
|
return v.check(input, err == nil, message)
|
||
|
}
|
||
|
|
||
|
func (v *Validator) check(field *Input, ok bool, message string) bool {
|
||
|
if !ok {
|
||
|
field.Error = errors.New(v.l.Get(message))
|
||
|
v.AllOK = false
|
||
|
}
|
||
|
return ok
|
||
|
}
|