I try to put 0 as value for a required in field and the it says missing required field
package main
import (
"fmt"
"gopkg.in/go-playground/validator.v9"
)
type StockPutForm struct {
Quantity int `validate:"required"`
}
func main() {
mystruct := StockPutForm{Quantity:0}
validate := validator.New()
err := validate.Struct(mystruct)
fmt.Println(err)
}
Key: 'StockPutForm.Quantity' Error:Field validation for 'Quantity' failed on the 'required' tag
0 is valid value for int then it should work.
Could you please help.
Thanks.
My bad, it's documented.
But as in #142 you removed exists validation
Is there any validation that check if value for field exists regardless of what it is?
It's also documented in the godocs under the required definition
Yes exists was removed because it was confusing, and functionality merged into required.
Just make your int a pointer and keep using required tag.
Just checking in, did you get this all working?
it works.
Thanks
@deankarn
what does it mean by "Just make your int a pointer and keep using required tag."?
@deankarn
what does it mean by "Just make your int a pointer and keep using required tag."?
It means that you should use a pointer to an integer instead of an integer itself.
From:
type Testing struct {
Delay int `validate:"required"`
}
To:
type Testing struct {
Delay *int `validate:"required"`
}
That way validate:"required" ensures that the pointer is not nil instead of ensuring the value of the integer is not 0.
Most helpful comment
It's also documented in the godocs under the
requireddefinitionYes exists was removed because it was confusing, and functionality merged into required.
Just make your int a pointer and keep using required tag.