Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

Replaced hardcoded message errors in email validation

Added some translation string
Improved the way the emails and usernames are checked (less conditions)
Minor fix on clear error functions
Cette révision appartient à :
akuma06 2017-06-10 00:58:28 +02:00
Parent 1675b12cad
révision ee0e611d11
4 fichiers modifiés avec 32 ajouts et 9 suppressions

Voir le fichier

@ -90,6 +90,7 @@ func SetCookieHandler(w http.ResponseWriter, r *http.Request, email string, pass
messages := msg.GetMessages(r) messages := msg.GetMessages(r)
// search by email or username // search by email or username
isValidEmail := formStruct.EmailValidation(email, messages) isValidEmail := formStruct.EmailValidation(email, messages)
messages.ClearErrors("email") // We need to clear the error added on messages
if isValidEmail { if isValidEmail {
if db.ORM.Where("email = ?", email).First(&user).RecordNotFound() { if db.ORM.Where("email = ?", email).First(&user).RecordNotFound() {
return http.StatusNotFound, errors.New("User not found") return http.StatusNotFound, errors.New("User not found")

Voir le fichier

@ -18,8 +18,10 @@ func EmailValidation(email string, mes *msg.Messages) bool {
if exp.MatchString(email) { if exp.MatchString(email) {
return true return true
} }
mes.AddErrorT("email", "email_not_valid")
return false
} }
mes.AddError("email", "Email Address is not valid") mes.AddError("errors", "Regexp couldn't be parsed!")
return false return false
} }
@ -28,13 +30,13 @@ func ValidateUsername(username string, mes *msg.Messages) bool {
exp, errorRegex := regexp.Compile(usernameRegex) exp, errorRegex := regexp.Compile(usernameRegex)
if regexpCompiled := log.CheckError(errorRegex); regexpCompiled { if regexpCompiled := log.CheckError(errorRegex); regexpCompiled {
if exp.MatchString(username) { if exp.MatchString(username) {
mes.AddError("username", "Username contains illegal characters") mes.AddErrorT("username", "username_illegal")
return false return false
} }
} else { return true
return false
} }
return true mes.AddError("errors", "Regexp couldn't be parsed!")
return false
} }
// IsAgreed : Check if terms and conditions are valid // IsAgreed : Check if terms and conditions are valid

Voir le fichier

@ -1214,5 +1214,13 @@
{ {
"id": "report_msg", "id": "report_msg",
"translation": "The torrent #%s has been reported!" "translation": "The torrent #%s has been reported!"
},
{
"id": "email_not_valid",
"translation": "Email Address is not valid!"
},
{
"id": "username_illegal",
"translation": "Username contains illegal characters!"
} }
] ]

Voir le fichier

@ -87,15 +87,27 @@ func (mes *Messages) AddInfoT(name string, id string) {
mes.AddInfo(name, mes.T(id)) mes.AddInfo(name, mes.T(id))
} }
// ClearErrors : Erase all errors in messages // ClearAllErrors : Erase all errors in messages
func (mes *Messages) ClearErrors() { func (mes *Messages) ClearAllErrors() {
mes.Errors = nil
mes.setMessagesInContext()
}
// ClearAllInfos : Erase all infos in messages
func (mes *Messages) ClearAllInfos() {
mes.Infos = nil mes.Infos = nil
mes.setMessagesInContext() mes.setMessagesInContext()
} }
// ClearErrors : Erase all errors in messages
func (mes *Messages) ClearErrors(name string) {
mes.Errors[name] = nil
mes.setMessagesInContext()
}
// ClearInfos : Erase all infos in messages // ClearInfos : Erase all infos in messages
func (mes *Messages) ClearInfos() { func (mes *Messages) ClearInfos(name string) {
mes.Errors = nil mes.Infos[name] = nil
mes.setMessagesInContext() mes.setMessagesInContext()
} }