form-handling.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package e00_basics
  2. // @snippet_begin(FormHandlingSample)
  3. import (
  4. "fmt"
  5. "io"
  6. "mime/multipart"
  7. "github.com/qor5/docs/docsrc/utils"
  8. "github.com/qor5/web"
  9. . "github.com/theplant/htmlgo"
  10. )
  11. type MyData struct {
  12. Text1 string
  13. Checkbox1 string
  14. Color1 string
  15. Email1 string
  16. Radio1 string
  17. Range1 int
  18. Url1 string
  19. Tel1 string
  20. Month1 string
  21. Time1 string
  22. Week1 string
  23. DatetimeLocal1 string
  24. File1 []*multipart.FileHeader
  25. HiddenValue1 string
  26. }
  27. func FormHandlingPage(ctx *web.EventContext) (pr web.PageResponse, err error) {
  28. var fv MyData
  29. err = ctx.UnmarshalForm(&fv)
  30. if fv.Text1 == "" {
  31. fv.Text1 = `Hello '1
  32. World`
  33. }
  34. if err != nil {
  35. panic(err)
  36. }
  37. pr.Body = Div(
  38. H1("Form Handling"),
  39. H3("Form Content"),
  40. utils.PrettyFormAsJSON(ctx),
  41. H3("File1 Content"),
  42. Pre(fv.File1Bytes()).Style("width: 400px; white-space: pre-wrap;"),
  43. Div(
  44. Label("Text1"),
  45. Input("").Type("text").Value(fv.Text1).Attr(web.VFieldName("Text1")...),
  46. ),
  47. Div(
  48. Label("Checkbox1"),
  49. Input("").Type("checkbox").Value("1").Checked(fv.Checkbox1 == "1").Attr(web.VFieldName("Checkbox1")...),
  50. ),
  51. web.Scope(
  52. Fieldset(
  53. Legend("Nested Form"),
  54. Div(
  55. Label("Color1"),
  56. Input("").Type("color").
  57. Value(fv.Color1).
  58. Attr(web.VFieldName("Color1")...),
  59. ),
  60. Div(
  61. Label("Email1"),
  62. Input("").Type("email").Value(fv.Email1).Attr(web.VFieldName("Email1")...),
  63. ),
  64. Input("").Type("checkbox").
  65. Attr("v-model", "locals.checked").
  66. Attr(web.VFieldName("Checked123")...),
  67. Button("Uncheck it").Attr("@click", "locals.checked = false"),
  68. Hr(),
  69. Button("Send").Attr("@click", web.POST().
  70. EventFunc("checkvalue").
  71. Query("id", 123).
  72. FieldValue("name", "azuma").
  73. Go()),
  74. ),
  75. ).VSlot("{ plaidForm, locals }").Init("{checked: true}"),
  76. web.Scope(
  77. Fieldset(
  78. Legend("Nested Form 2"),
  79. Div(
  80. Label("Email1"),
  81. Input("").Type("email").Value(fv.Email1).Attr(web.VFieldName("Email1")...),
  82. ),
  83. Button("Send").Attr("@click", web.POST().
  84. EventFunc("checkvalue").
  85. Go()),
  86. ),
  87. ).VSlot("{ plaidForm, locals }").Init("{checked: true}"),
  88. Div(
  89. Fieldset(
  90. Legend("Radio"),
  91. Label("Radio Value 1"),
  92. Input("Radio1").Type("radio").
  93. Value("1").Checked(fv.Radio1 == "1").Attr(web.VFieldName("Radio1")...),
  94. Label("Radio Value 2"),
  95. Input("Radio1").Type("radio").
  96. Value("2").Checked(fv.Radio1 == "2").Attr(web.VFieldName("Radio1")...),
  97. ),
  98. ),
  99. Div(
  100. Label("Range1"),
  101. Input("").Type("range").Value(fmt.Sprint(fv.Range1)).Attr(web.VFieldName("Range1")...),
  102. ),
  103. web.Scope(
  104. Div(
  105. Label("Url1"),
  106. Input("").Type("url").Value(fv.Url1).Attr(web.VFieldName("Url1")...),
  107. ),
  108. Div(
  109. Label("Tel1"),
  110. Input("").Type("tel").Value(fv.Tel1).Attr(web.VFieldName("Tel1")...),
  111. ),
  112. Div(
  113. Label("Month1"),
  114. Input("").Type("month").Value(fv.Month1).Attr(web.VFieldName("Month1")...),
  115. ),
  116. ).VSlot("{ locals }"),
  117. Div(
  118. Label("Time1"),
  119. Input("").Type("time").Value(fv.Time1).Attr(web.VFieldName("Time1")...),
  120. ),
  121. Div(
  122. Label("Week1"),
  123. Input("").Type("week").Value(fv.Week1).Attr(web.VFieldName("Week1")...),
  124. ),
  125. Div(
  126. Label("DatetimeLocal1"),
  127. Input("").Type("datetime-local").Value(fv.DatetimeLocal1).Attr(web.VFieldName("DatetimeLocal1")...),
  128. ),
  129. Div(
  130. Label("File1"),
  131. Input("").Type("file").Value("").Attr(web.VFieldName("File1")...),
  132. ),
  133. Div(
  134. Label("Hidden values with default"),
  135. Input("").Type("hidden").Value(`hidden value
  136. '123`).Attr(web.VFieldName("HiddenValue1")...),
  137. ),
  138. Div(
  139. Button("Submit").Attr("@click", web.POST().EventFunc("checkvalue").Go()),
  140. ),
  141. )
  142. return
  143. }
  144. func checkvalue(ctx *web.EventContext) (er web.EventResponse, err error) {
  145. er.Reload = true
  146. return
  147. }
  148. func (m *MyData) File1Bytes() string {
  149. if m.File1 == nil || len(m.File1) == 0 {
  150. return ""
  151. }
  152. f, err := m.File1[0].Open()
  153. if err != nil {
  154. panic(err)
  155. }
  156. var b = make([]byte, 200)
  157. _, err = io.ReadFull(f, b)
  158. if err != nil {
  159. panic(err)
  160. }
  161. return fmt.Sprintf("%+v ...", b)
  162. }
  163. var FormHandlingPagePB = web.Page(FormHandlingPage).
  164. EventFunc("checkvalue", checkvalue)
  165. const FormHandlingPagePath = "/samples/form_handling"
  166. // @snippet_end