page.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package e11_vuetify_basic_inputs
  2. // @snippet_begin(VuetifyBasicInputsSample)
  3. import (
  4. "mime/multipart"
  5. "github.com/qor5/docs/docsrc/utils"
  6. . "github.com/qor5/ui/vuetify"
  7. "github.com/qor5/web"
  8. h "github.com/theplant/htmlgo"
  9. )
  10. type myFormValue struct {
  11. MyValue string
  12. TextareaValue string
  13. Gender string
  14. Agreed bool
  15. Feature1 bool
  16. Slider1 int
  17. PortalAddedValue string
  18. Files1 []*multipart.FileHeader
  19. Files2 []*multipart.FileHeader
  20. Files3 []*multipart.FileHeader
  21. }
  22. var s = &myFormValue{
  23. MyValue: "123",
  24. TextareaValue: "This is textarea value",
  25. Gender: "M",
  26. Agreed: false,
  27. Feature1: true,
  28. Slider1: 60,
  29. }
  30. func VuetifyBasicInputs(ctx *web.EventContext) (pr web.PageResponse, err error) {
  31. var verr web.ValidationErrors
  32. if ve, ok := ctx.Flash.(web.ValidationErrors); ok {
  33. verr = ve
  34. }
  35. pr.Body = VContainer(
  36. utils.PrettyFormAsJSON(ctx),
  37. VTextField().
  38. Label("Form ValueIs").
  39. Solo(true).
  40. Clearable(true).
  41. FieldName("MyValue").
  42. ErrorMessages(verr.GetFieldErrors("MyValue")...).
  43. Value(s.MyValue),
  44. VTextarea().FieldName("TextareaValue").
  45. ErrorMessages(verr.GetFieldErrors("TextareaValue")...).
  46. Solo(true).Value(s.TextareaValue),
  47. VRadioGroup(
  48. VRadio().Value("F").Label("Female"),
  49. VRadio().Value("M").Label("Male"),
  50. ).FieldName("Gender").Value(s.Gender),
  51. VCheckbox().FieldName("Agreed").
  52. ErrorMessages(verr.GetFieldErrors("Agreed")...).
  53. Label("Agree").InputValue(s.Agreed),
  54. VSwitch().FieldName("Feature1").InputValue(s.Feature1),
  55. VSlider().FieldName("Slider1").
  56. ErrorMessages(verr.GetFieldErrors("Slider1")...).
  57. Value(s.Slider1),
  58. web.Portal().Name("Portal1"),
  59. VFileInput().FieldName("Files1"),
  60. VFileInput().Label("Auto post to server after select file").Multiple(true).
  61. Attr("@change", web.POST().
  62. EventFunc("update").
  63. FieldValue("Files2", web.Var("$event")).
  64. Go()),
  65. h.Div(
  66. h.Input("Files3").Type("file").
  67. Attr("@input", web.POST().
  68. EventFunc("update").
  69. FieldValue("Files3", web.Var("$event")).
  70. Go()),
  71. ).Class("mb-4"),
  72. VBtn("Update").OnClick("update").Color("primary"),
  73. h.P().Text("The following button will update a portal with a hidden field, if you click this button, and then click the above update button, you will find additional value posted to server"),
  74. VBtn("Add Portal Hidden Value").OnClick("addPortal"),
  75. )
  76. return
  77. }
  78. func addPortal(ctx *web.EventContext) (r web.EventResponse, err error) {
  79. r.UpdatePortals = append(r.UpdatePortals, &web.PortalUpdate{
  80. Name: "Portal1",
  81. Body: h.Input("").Type("hidden").Value("this is my portal added hidden value").Attr(web.VFieldName("PortalAddedValue")...),
  82. })
  83. return
  84. }
  85. func update(ctx *web.EventContext) (r web.EventResponse, err error) {
  86. s = &myFormValue{}
  87. ctx.MustUnmarshalForm(s)
  88. verr := web.ValidationErrors{}
  89. if len(s.MyValue) < 10 {
  90. verr.FieldError("MyValue", "my value is too small")
  91. }
  92. if len(s.TextareaValue) > 5 {
  93. verr.FieldError("TextareaValue", "textarea value is too large")
  94. }
  95. if !s.Agreed {
  96. verr.FieldError("Agreed", "You must agree the terms")
  97. }
  98. if s.Slider1 > 50 {
  99. verr.FieldError("Slider1", "You slide too much")
  100. }
  101. ctx.Flash = verr
  102. r.Reload = true
  103. return
  104. }
  105. var VuetifyBasicInputsPB = web.Page(VuetifyBasicInputs).
  106. EventFunc("update", update).
  107. EventFunc("addPortal", addPortal)
  108. // @snippet_end
  109. const VuetifyBasicInputsPath = "/samples/vuetify-basic-inputs"