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

Allow date inputs to default to blank #152

Closed
myronmarston opened this issue Dec 10, 2009 · 9 comments
Closed

Allow date inputs to default to blank #152

myronmarston opened this issue Dec 10, 2009 · 9 comments
Labels

Comments

@myronmarston
Copy link

Currently there is no way to make a date input default to blank. You can pass a default using the :selected option, but it only uses this if ruby evaluates it to true. Otherwise if uses Time.now. So, if you pass :selected => nil, ruby considers this false and Time.now is used.

See discussion at:

http://groups.google.com.au/group/formtastic/browse_thread/thread/5ffd565ddcad412f

@justinfrench
Copy link
Member

I have pushed up a fix for this in http://github.com/justinfrench/formtastic/commit/7b947cb528e1e13c1f62da23be07ba310ff2c1bd.

I only partially implemented Adam's suggested fix. We now only fallback to Time.now if options[:selected] is not present, but I didn't think it was necessary to force a blank option when selected is nil — that should be the form author's choice.

Closing for now, will be in next gem.

@twalpole
Copy link
Contributor

Shouldnt options[:selected] override the value returned by the method on the object?. With this fix that wont happen for options[:selected]=nil since its OR'd with the value returned

@justinfrench
Copy link
Member

So, let me get this straight:

  • if :selected is provided as a Date or Time, regardless of the value in the object, we should pre-select that value
  • if :selected is provided as nil, regardless of the value of the object, nothing should be selected
  • if :selected is not provided, and the object has a value, we should pre-select that
  • if :selected is not provided, and the object has no value, nothing should be selected

Is that the use cases I need to check/spec?

@myronmarston
Copy link
Author

That sounds OK to me. For the 4th one, you may want it to select Time.now. That's the old default behavior and I imagine some people are relying upon that behavior. The main thing is to honor whatever value is passed for the :selected option. The default behavior (when no :selected option is provided) can go either way--just make sure it's documented.

@twalpole
Copy link
Contributor

I believe the problem here is that :selected is being used to set the :default behavior, when the two typically have different meanings. :selected is typically to override what would normally be set, while :default would be to set what is selected when nothing is set. Maybe there should be an option to set :default (which would default to Time.now if not set). Then it would be

  • if :selected is provided as a Date or Time, regardless of the value in the object, we should pre-select that value
  • if :selected is provided as nil, regardless of the value of the object, nothing should be selected
  • if :selected is not provided, and the object has a value, we should pre-select that
  • if :selected is not provided, and the object has no value, and :default is provided then :default should be pre-selected
  • if :selected is not provided, and the object has no value, and :default is not provided then Time.now should be selected

@justinfrench
Copy link
Member

Sigh good advice. I'm a bit shitty about this because the only reason I'm supporting it is because Rails does. I think this stuff should all be done when initializing the model.

Anyway, thanks, looks like we have the use cases sorted, I'll go re-write about 50 lines of specs for three lines of code changes ;)

@justinfrench
Copy link
Member

We've had more discussion in the google group, I'm leaning towards :default instead of :selected.

http://groups.google.com.au/group/formtastic/msg/ab160fb984ba1529

@justinfrench
Copy link
Member

Ok, I have a patch in the "datetime" branch, would love some code review and real-world testing before it goes into master:

http://github.com/justinfrench/formtastic/tree/datetime
http://github.com/justinfrench/formtastic/commit/326b556ceb0fc6d0761a8a451507abe986e8fd9b

@justinfrench
Copy link
Member

A few people have reviewed this, closing.

zuf pushed a commit to zuf/formtastic that referenced this issue Sep 7, 2012
…t to nil):

* added failing specs
* changed date_or_datetime_input to only fallback to Time.now when options[:selected] is not present
zuf pushed a commit to zuf/formtastic that referenced this issue Sep 7, 2012
…avour of a deprecated :selected option (issue formtastic#152):

* if the object has a value, we should pre-select that at all times, ignoring the :default option
* if the object has no value, and :default is provided as a Date/Time then :default should be pre-selected
* if the object has no value, and :default is provided as nil then no option should be pre-selected (it will default to the first)
* if the object has no value, and :default is not provided then fallback to Time.now (or perhaps a configurable default)
* :selected invokes a deprecation warning, but is an alias of :default
* it's not a real deprecation, as the behaviour has changed with all the bugs and half-fixes
This issue was closed.
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

3 participants