These are chat archives for gin-gonic/gin

31st
Jan 2019
dacrypt
@dacrypt
Jan 31 02:17
HI, a bit desperated here...I'm having issues trying to c.Bind(&nestedStruct) when the request is a json payload. However, It does work fine if the request is made via form-urlencoded. Any ideas?
the problem is json won't bind to the nested struct completely, just the main struct.
dacrypt
@dacrypt
Jan 31 02:49
// User is the model for the user table.
type User struct {
    Base
    Phone    string `gorm:"column:phone;varchar(15);not null;unique_index" form:"phone" json:"phone" bindind:"required"`
    Password string `gorm:"column:password;not null;" form:"password" json:"password" bindind:"required"`
    Profile Profile `json:"profile" binding:"required,dive"`
}
// Profile is the model for the profile table.
type Profile struct {
    Base
    FirstName string `form:"first_name" json:"first_name"`
    LastName  string `form:"last_name" json:"last_name"`
    Email     string `gorm:"type:varchar(110);" form:"email" json:"email"`
}

And the post body payload (JSON) is:

{
    "phone": "99345678918",
    "first_name": "David4",
    "last_name": "Velasquez1",
    "password": "1234",
    "email": "c",
}

the code:

// Get the user fields from the request and bind them to a user struct
    user := &models.User{}
    err := c.ShouldBind(user)

This will bind only phoneand password but not the others