Form validation
This commit is contained in:
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var pcInteractive templates.PageContext = templates.PageContext{
|
||||
Title: "Welcome to the demo",
|
||||
Title: "Welcome to the demo - Interactive",
|
||||
ActivePage: "interactive",
|
||||
}
|
||||
|
||||
@ -50,3 +50,38 @@ func FilterCatBreeds(c *gin.Context) {
|
||||
|
||||
templates.RenderCatBreedsTable(catBreeds).Render(c, c.Writer)
|
||||
}
|
||||
|
||||
func ValidateForm(c *gin.Context) {
|
||||
content := templates.ValidateFormContent{
|
||||
Validated: true,
|
||||
NumValue: c.PostForm("number-value"),
|
||||
StrValue: c.PostForm("string-value"),
|
||||
}
|
||||
|
||||
numValue, err := strconv.Atoi(content.NumValue)
|
||||
if err != nil {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "This is not valid number"
|
||||
}
|
||||
if !content.HasNumValueError {
|
||||
if numValue < 0 {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "Value is less than 0"
|
||||
}
|
||||
if numValue > 100 {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "Value is greater than 100"
|
||||
}
|
||||
}
|
||||
|
||||
if len(content.StrValue) < 5 {
|
||||
content.HasStrValueError = true
|
||||
content.StrValueError = "String length is less than 5"
|
||||
}
|
||||
if len(content.StrValue) > 10 {
|
||||
content.HasStrValueError = true
|
||||
content.StrValueError = "String length is more than 10"
|
||||
}
|
||||
|
||||
templates.RenderInteractiveForm(content).Render(c, c.Writer)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user