Giter Site home page Giter Site logo

validations's Introduction

Validations

Validations provides a means to validate GORM models when creating and updating them.

Register GORM Callbacks

Validations uses GORM callbacks to handle validations, so you will need to register callbacks first:

import (
  "github.com/jinzhu/gorm"
  "github.com/qor/validations"
)

func main() {
  db, err := gorm.Open("sqlite3", "demo_db")

  validations.RegisterCallbacks(db)
}

Usage

After callbacks have been registered, attempting to create or update any record will trigger the Validate method that you have implemented for your model. If your implementation adds or returns an error, the attempt will be aborted.

type User struct {
  gorm.Model
  Age uint
}

func (user User) Validate(db *gorm.DB) {
  if user.Age <= 18 {
    db.AddError(errors.New("age need to be 18+"))
  }
}

db.Create(&User{Age: 10})         // won't insert the record into database, as the `Validate` method will return error

var user User{Age: 20}
db.Create(&user)                  // user with age 20 will be inserted into database
db.Model(&user).Update("age", 10) // user's age won't be updated, will return error `age need to be 18+`

// If you have added more than one error, could get all of them with `db.GetErrors()`
func (user User) Validate(db *gorm.DB) {
  if user.Age <= 18 {
    db.AddError(errors.New("age need to be 18+"))
  }
  if user.Name == "" {
    db.AddError(errors.New("name can't be blank"))
  }
}

db.Create(&User{}).GetErrors() // => []error{"age need to be 18+", "name can't be blank"}

Govalidator integration

Qor Validations supports govalidator, so you could add a tag into your struct for some common validations, such as check required, numeric, length, etc.

type User struct {
  gorm.Model
  Name           string `valid:"required"`
  Password       string `valid:"length(6|20)"`
  SecurePassword string `valid:"numeric"`
  Email          string `valid:"email"`
}

Customize errors on form field

If you want to display errors for each form field in QOR Admin, you could register your error like this:

func (user User) Validate(db *gorm.DB) {
  if user.Age <= 18 {
    db.AddError(validations.NewError(user, "Age", "age need to be 18+"))
  }
}

Try it out for yourself

Checkout the http://demo.getqor.com/admin/products/1 demo, change Name to be a blank string and save to see what happens.

License

Released under the MIT License.

validations's People

Contributors

binku87 avatar bodhi avatar jinzhu avatar raven-chen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.