tags icon indicating copy to clipboard operation
tags copied to clipboard

Add support for nested values

Open markbates opened this issue 8 years ago • 5 comments

If a Person has a Company on it, it would be great to be able to create a "sub form" for the Company.

markbates avatar Mar 21 '17 16:03 markbates

@markbates should we start thinking on adding support for https://github.com/monoculum/formam instead of schema ?

paganotoni avatar Apr 05 '17 01:04 paganotoni

That's where I'm leaning. What are your thoughts?

markbates avatar Apr 05 '17 01:04 markbates

I've been having this sort of trouble (with schema) trying to implement an array value passed by the view, i've not had the chance to play with formam and have not had the chance to send a PR to schema, but just google and they seem to be looking for this kind of feature:

https://github.com/gorilla/schema/issues/24

WDYT if we do some effort and attempt to add arrays and maps support into schema ? my only fear on moving into formam is that schema seems more mature.

paganotoni avatar Apr 05 '17 01:04 paganotoni

Here's the code that caused me to switch off of schema:

import "github.com/markbates/pop/slices"
type Contact struct {
...
	Interests   slices.Map `json:"interests" db:"interests"`
...
}
<h1>Contact</h1>

<%= form_for(contact, {action: "/contact", errors: errors}) { %>
...
  <label>Please check the topics that are of interest for you.</label>
  <div class="row">
    <%= for (g) in groupBy(2, interests) { %>
      <div class="col-md-6">
        <%= for (i, n) in g { %>
          <%= f.CheckboxTag("Interests", {name: "Interests["+n+"]", value: contact.Interests[n], label: n, checked: n}) %>
        <% } %>
      </div>
    <% } %>
  </div>
...
<% } %>

I couldn't probably handle the map in the contact struct. In the database it is stored as JSON. Schema couldn't handle it all, formam had no problem doing it. I just needed to register a different binder for it:

func init() {
	buffalo.RegisterBinder("application/x-www-form-urlencoded", func(req *http.Request, i interface{}) error {
		err := req.ParseForm()
		if err != nil {
			return errors.WithStack(err)
		}
		dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "formam"})
		if err := dec.Decode(req.Form, i); err != nil {
			return errors.WithStack(err)
		}
		return nil
	})
}

markbates avatar Apr 05 '17 01:04 markbates

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment. Otherwise, this will be closed in 7 days.

github-actions[bot] avatar Oct 05 '22 03:10 github-actions[bot]