top
(README.md)
# Validations

Validations provides a means to validate [GORM-backend](https://github.com/jinzhu/gorm) models when creating and updating them.

### Register GORM Callbacks

Validations uses [GORM](https://github.com/jinzhu/gorm) callbacks to handle validations, so you will need to register callbacks first:

```go
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.

```go
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"}
```

Qor Validation add support for [govalidator](https://github.com/asaskevich/govalidator), you could add tag into 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"`
}
```

## [Qor Support](https://github.com/qor/qor)

[QOR](http://getqor.com) is architected from the ground up to accelerate development and deployment of Content Management Systems, E-commerce Systems, and Business Applications and as such is comprised of modules that abstract common features for such systems.

Validations could be used alone, but it works very nicely with QOR (as a QOR Plugin) - if you have requirements to manage your application's data be sure to check QOR out!

[QOR Demo:  http://demo.getqor.com/admin](http://demo.getqor.com/admin)

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

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

Checkout [http://demo.getqor.com/admin/products/1](http://demo.getqor.com/admin/products/1) as demo, change `Name` to be a blank string and save to see what happens.

## License

Released under the [MIT License](http://opensource.org/licenses/MIT).

Imported by 84 package(s)

  1. github.com/8legd/HugoCMS/qor
  2. github.com/8legd/hugocms/qor
  3. github.com/GazeboXu/qor-example/app/account
  4. github.com/GazeboXu/qor-example/app/admin
  5. github.com/GazeboXu/qor-example/app/stores
  6. github.com/GazeboXu/qor-example/config/db
  7. github.com/GazeboXu/qor-example/models/products
  8. github.com/Jungju/qor/resource
  9. github.com/Kiennh/activity
  10. github.com/andboson/qor-admin-test/app/models
  11. github.com/andboson/qor-admin-test/db
  12. github.com/azumads/selenium/app
  13. github.com/azumads/utils
  14. github.com/bigpigeon/qor/resource
  15. github.com/chazzuka/qor-example/app/models
  16. github.com/chazzuka/qor-example/config/admin
  17. github.com/chazzuka/qor-example/db
  18. github.com/dwarvesf/delivr-admin/app/model
  19. github.com/dwarvesf/delivr-admin/db
  20. github.com/dwarvesf/qor/resource
  21. github.com/harborapp/harbor-api/store/data
  22. github.com/huacnlee/mediom/app
  23. github.com/jungju/qor/resource
  24. github.com/kapmahc/qor/resource
  25. github.com/kiennh/activity
  26. github.com/kleister/kleister-api/store/data
  27. github.com/mmanjoura/cms-api/app/account
  28. github.com/mmanjoura/cms-api/app/admin
  29. github.com/mmanjoura/cms-api/app/stores
  30. github.com/mmanjoura/cms-api/config/db
  31. github.com/mmanjoura/cms-api/models/products
  32. github.com/mmanjoura/qor-app/activity
  33. github.com/mmanjoura/qor-app/exchange
  34. github.com/mmanjoura/qor-app/publish2
  35. github.com/mmanjoura/qor-app/qor-example/config/db
  36. github.com/mmanjoura/qor-app/qor/resource
  37. github.com/mmanjoura/qor-app/slug
  38. github.com/mmanjoura/qor-example/app/models
  39. github.com/mmanjoura/qor-example/config/admin
  40. github.com/mmanjoura/qor-example/db
  41. github.com/mstat/qor-example/app/models
  42. github.com/mstat/qor-example/config/admin
  43. github.com/mstat/qor-example/db
  44. github.com/mstat/qor/resource
  45. github.com/oskca/qor/resource
  46. github.com/oywc410/qor-example/app/models
  47. github.com/oywc410/qor-example/config/admin
  48. github.com/oywc410/qor-example/db
  49. github.com/qor/activity
  50. github.com/qor/exchange
  51. github.com/qor/publish2
  52. github.com/qor/qor-example/app/account
  53. github.com/qor/qor-example/app/admin
  54. github.com/qor/qor-example/app/models
  55. github.com/qor/qor-example/app/stores
  56. github.com/qor/qor-example/config/admin
  57. github.com/qor/qor-example/config/db
  58. github.com/qor/qor-example/db
  59. github.com/qor/qor-example/models/products
  60. github.com/qor/qor/resource
  61. github.com/qor/slug
  62. github.com/raven-chen/qor-doc-demo/db
  63. github.com/reechou/erp/app/models
  64. github.com/reechou/erp/config/admin
  65. github.com/reechou/erp/db
  66. github.com/reechou/qor-example/db
  67. github.com/reechou/qor/resource
  68. github.com/reechou/real-erp/admin
  69. github.com/reechou/real-erp/models
  70. github.com/solderapp/solder-api/store/data
  71. github.com/solderapp/solder/store/data
  72. github.com/steveoc64/publish2
  73. github.com/steveoc64/qor-example/app/models
  74. github.com/steveoc64/qor-example/config/admin
  75. github.com/steveoc64/qor-example/db
  76. github.com/umschlag/umschlag-api/pkg/storage
  77. github.com/umschlag/umschlag-api/store/data
  78. github.com/whimsycwd/qor-example/app/models
  79. github.com/whimsycwd/qor-example/config/admin
  80. github.com/whimsycwd/qor-example/db
  81. github.com/youryharchenko/qor-example/app/models
  82. github.com/youryharchenko/qor-example/config/admin
  83. github.com/youryharchenko/qor-example/db
  84. gopkg.in/kleister/kleister-api.v0/store/data

Imported only in test by 1 package(s)

  1. github.com/mmanjoura/qor-app/validations

Imports 2 package(s)

  1. github.com/asaskevich/govalidator
  2. github.com/jinzhu/gorm

Test imports 2 package(s)

  1. github.com/mattn/go-sqlite3
  2. github.com/qor/qor/test/utils