Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested values #11

Open
markbates opened this issue Mar 21, 2017 · 5 comments
Open

Add support for nested values #11

markbates opened this issue Mar 21, 2017 · 5 comments
Labels
enhancement New feature or request s: triage
Milestone

Comments

@markbates
Copy link
Member

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

@paganotoni
Copy link
Member

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

@markbates
Copy link
Member Author

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

@paganotoni
Copy link
Member

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:

gorilla/schema#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.

@markbates
Copy link
Member Author

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

@sio4 sio4 added enhancement New feature or request s: triage labels Sep 4, 2022
@github-actions
Copy link

github-actions bot commented Oct 5, 2022

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 github-actions bot added the stale label Oct 5, 2022
@sio4 sio4 added this to the Proposal milestone Oct 5, 2022
@sio4 sio4 removed the stale label Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request s: triage
Projects
None yet
Development

No branches or pull requests

3 participants