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

Changing how inputs with Array attributes are generated. #1523

Merged
merged 1 commit into from Jun 9, 2021

Conversation

jwoertink
Copy link
Member

Purpose

Related #1518

Description

We already treat the param names ending with [] as an array, but right now, there's no way to tell your input that it's an array without manually updating the name. This PR looks at the field, and if it's an Array type, we can append [] to the name. This required a few things to change since these would call field.param which would just call value.to_s if it was nil. In the case of an array, this would make the value of the input "[]".

Now we can keep track of how many times you call the attribute which allows us to set a different value for all of the array values you have.

op.tags.each do |tag|
  span tag
  text_input(op.tags, value: tag)
end
span "Add new tag"
text_input(op.tags)

Note

I've tagged this as "BREAKING CHANGE" because this will change how your forms will work when you have arrays. The other major deal is Avram still doesn't handle pulling in values from array params.

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

@jwoertink jwoertink added the BREAKING CHANGE This will cause a breaking change label Jun 8, 2021
@jwoertink jwoertink mentioned this pull request Jun 8, 2021
5 tasks
@matthewmcgarvey
Copy link
Member

I'm having a hard to understanding when/how this would be used. What's the counter for? Why are you making multiple inputs instead of like multiple options for a dropdown? Could you explain a bit more?

@jwoertink
Copy link
Member Author

The use of the inputs is mostly arbitrary, but you could say that you are letting an admin create new tags on a product, and each tag gets its own text_input. This PR is mainly about passing in an Array attribute, and it generating an input name with [] appended on to it.

I could use checkbox too, but currently if you had tags = ["footwear", "on sale"], and passed that attribute to checkbox, you'd get

<input type="checkbox" name="product:tags" value="[\"footwear\", \"on sale\"]">

Now with this PR, you can get:

<input type="checkbox" name="product:tags[]" value="footwear">
<input type="checkbox" name="product:tags[]" value="on sale">

@jwoertink jwoertink merged commit f1da2e0 into master Jun 9, 2021
@jwoertink jwoertink deleted the array_inputs branch June 9, 2021 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE This will cause a breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants