Dionys Rosario
Dionys Rosario

Reputation: 1

Beego validation accepts invalid data

I am trying to validate some forms using Beego validation, but it is not working at all: invalid data passes without errors.

This the relevant code, I don't know what is wrong. Can you point me at the mistake?

https://github.com/dionyself/golang-cms/blob/master/models/form.go

package models

import (
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/validation"
)

type BaseForm struct {
    Errors map[string]string
}

func (form *BaseForm) Validate() bool {
    valid := validation.Validation{}
    b, err := valid.Valid(form)
    if err != nil {
        beego.Error(err)
    }
    if !b {
        for _, err := range valid.Errors {
            form.Errors[err.Key] = err.Message
            beego.Debug(err.Key, err.Message)
        }
    }
    return b
}

type RegisterForm struct {
    BaseForm
    Username   string `form:"username" valid:"Required; AlphaNumeric; MinSize(4); MaxSize(300)"`
    Password   string `form:"password" valid:"Required; MinSize(4); MaxSize(30)"`
    PasswordRe string `form:"passwordre" valid:"Required; MinSize(4); MaxSize(30)"`
}

func (form *RegisterForm) Valid(v *validation.Validation) {
    // Check if passwords of two times are same.
    if form.Password != form.PasswordRe {
        v.SetError("PasswordRe", "Passwords did not match")
        return
    }
}

type ArticleForm struct {
    BaseForm
    Id            int    `form:"-"`
    Title         string `form:"title" valid:"Required;MinSize(4);MaxSize(300)"`
    Category      int    `form:"category"`
    Content       string `form:"content" valid:"Required; MinSize(50); MaxSize(2000)"`
    TopicTags     string `form:"topic-tags" valid:"MinSize(4); MaxSize(300)"`
    TaggedUsers   string `form:"tagged-users" valid:"MinSize(4); MaxSize(300)"`
    AllowReviews  bool   `form:"allow-reviews" valid:"Required"`
    AllowComments bool   `form:"allow-comments" valid:"Required"`
    Errors        map[string]string
}

func (form *ArticleForm) Valid(v *validation.Validation) {
    if form.Category >= 0 {
        v.SetError("Category", "Invalid category")
        return
    }
}

Some documentation: http://beego.me/docs/mvc/controller/validation.md

This is the code that parses the form:

func (this *ArticleController) Post() {
    form := models.ArticleForm{}
    Art := new(models.Article)
    if err := this.ParseForm(&form); err != nil {
        this.Abort("401")
    } else {
        db := this.GetDB()
        if !form.Validate() {
            this.Data["form"] = form
            var cats []*models.Category
            db.QueryTable("category").All(&cats)
            this.Data["Categories"] = cats
            this.ConfigPage("article-editor.html")
            for key, msg := range form.Errors {
                fmt.Println(key, msg)
            }
        } else {
            db.Insert(Art)
            this.Data["Article"] = Art
            this.ConfigPage("article.html")
        }
    }
}

Note: FormData is always accepted (even an empty form), form.Validate() is always returning 'true'... 0 errors on logs.

Upvotes: 0

Views: 1218

Answers (1)

Patrick Mark Mazo
Patrick Mark Mazo

Reputation: 21

It's because your struct has a data type with map[string]interface{} which accepts any data type and converting it into a string try to be specific in data type

Upvotes: 0

Related Questions