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

Set hidden input’s name using visible input - and remove visible input’s name #377

Closed
psorowka opened this issue Mar 28, 2014 · 8 comments
Labels
Milestone

Comments

@psorowka
Copy link

Say we have the following minimal form:

<form>
  <input type="date" name="the_date" />
  <input type="submit" />
</form>

Adding datepicker will extend this form like this:

<form>
  <input type="date" name="the_date" />
  <input type="hidden" name="the_date_submit" />
  <input type="submit" />
</form>

This forces the server-side to be kind of aware of pickadate. It also does not allow progressive enhancement. IMHO the form should be more like:

<form>
  <input type="date" name="display_the_date" />
  <input type="hidden" name="the_date" />
  <input type="submit" />
</form>

In order to not break current behavior, adding an option 'displaySuffix' and 'displayPrefix' would be an idea.

Also it would be cool when then suffix would respect array notation. In a rails app i usually have input names like:

<input type="date" name="reservation[date]" />

Appending the suffix like this:

<input type="date" name="reservation[date]_submit" /> 

breaks the form. Prefereable would be

<input type="date" name="reservation[date_submit]" /> 

Should I implement those features and drop in a pull-request?

@amsul
Copy link
Owner

amsul commented Apr 2, 2014

@psorowka I like this idea of renaming the original input's name! Although I would probably reduce it down to one option that decides which input gets the prefix & suffix.

In regards to the array notation for rails, there's already a PR for it: #342

@psorowka
Copy link
Author

psorowka commented Apr 2, 2014

After thinking a bit more about this, I now think we should go a step further: Just remove the name attribute from the display-input and attach it to the hidden input. No need for prefix/suffix then.
The human-friendly-formatted date usually doesnt need to be posted at all.

@amsul
Copy link
Owner

amsul commented Apr 2, 2014

True..well now that you mentioned that, nothing really needs to be changed at all.

You can simply remove the hidden suffix and prefix by setting them to an empty string - thereby making both input fields use the same name. The last named value is what gets sent to the server, which will always be the hidden input since that appears after the visible input in the DOM.

@psorowka
Copy link
Author

psorowka commented Apr 2, 2014

Although you are basically right and this is the way I am currently doing it already; this seems to be semantically wrong. Actually - as far as I know - both values will be send to the server, but usual servers will overwrite previous field-values if the same field-name occurs twice. So: yes, no urgent action needed. But from a semantical point of view I would prefer to remove the name= from the non-data-relevant input.

@psorowka psorowka closed this as completed Apr 2, 2014
@amsul
Copy link
Owner

amsul commented Apr 2, 2014

Sorry, you're right - both values are still sent, but yes servers usually do remedy duplicate names by resolving to the last value.

And yeah, you are right about it not being a totally appropriate solution to the issue. So I'm going to reopen and add an option to cut the input's name onto the hidden one instead.

I'd assume the expected behavior is that setting that option to true would essentially nullify the hidden prefix and suffix?

@amsul amsul reopened this Apr 2, 2014
@amsul amsul added todo labels Apr 2, 2014
@amsul amsul added this to the v3.4.1 milestone Apr 2, 2014
@psorowka
Copy link
Author

psorowka commented Apr 2, 2014

yes cool, agreed on that

@amsul amsul changed the title Rename displayed input rather than submit-input Set hidden input’s name using visible input - and remove visible input’s name Apr 12, 2014
@amsul amsul removed the todo label Apr 12, 2014
@amsul amsul closed this as completed in ff0cc0c Apr 13, 2014
@javieracrich
Copy link

I have 2 date pickers (from and until). They are both required. I'm using the "required" html5 attribute for both display inputs but since the datepicker removed the 'name' attribute, the required validation only works in the first picker. not both. Is there any alternative for this scenario to work?

@harmdewit
Copy link

Nice solution, seems a better solution than the hidden prefixes/suffixes. Could this please become the default?

I can imagine this makes it harder to upgrade, but new people with ruby/rails seem to keep running into this issue because root problem is unclear, see also veracross/pickadate-rails#13 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants