func (v *NewPerson) CreatePerson(ev dom.Event) { ev.PreventDefault() formEl, ok := ev.CurrentTarget().(*dom.HTMLFormElement) if !ok { panic("Could not cast target to dom.HTMLFormElement: " + fmt.Sprintf("%T", ev.CurrentTarget())) } f, err := form.Parse(formEl) if err != nil { panic(err) } f.Validate("name").Required() f.Validate("age").Required().IsInt().Greater(0) if f.HasErrors() { if errorsView == nil { errorsView = NewErrors(f.Errors) view.InsertBefore(errorsView, v) } errorsView.Errors = f.Errors if err := errorsView.Render(); err != nil { panic(err) } return } person := &models.Person{} if err := f.Bind(person); err != nil { panic(err) } restClient := rest.NewClient() restClient.ContentType = rest.ContentJSON go func() { if err := restClient.Create(person); err != nil { if httpErr, ok := err.(rest.HTTPError); ok { fmt.Println(string(httpErr.Body)) } panic(err) } v.Router.Navigate("/people") }() }
package controllers import ( "log" "github.com/go-humble/examples/people/client/views" "github.com/go-humble/examples/people/shared/models" "github.com/go-humble/rest" "github.com/go-humble/router" ) type People struct { Router *router.Router } var client = rest.NewClient() func (p People) New(context *router.Context) { newPersonView := views.NewNewPerson(nil, p.Router) if context.InitialLoad { newPersonView.DelegateEvents() return } if err := newPersonView.Render(); err != nil { log.Fatal(err) } } func (p People) Index(context *router.Context) { if context.InitialLoad { return