Form validation

This commit is contained in:
Eden Kirin
2024-01-21 21:06:26 +01:00
parent eda1d02418
commit abde90ee33
7 changed files with 407 additions and 89 deletions

View File

@ -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)
}