albertopq / jquery_datepicker

Rails 3 plugin that allows you to select dates from a calendar
91 stars 60 forks source link

defaultDate/value issue #16

Closed ltrevisanDU closed 12 years ago

ltrevisanDU commented 12 years ago

After a few tweaks, I was finally able to make the datepicker show up and change the date in the field. However when coupled with a submit button, custom name (without a value or default date set), I started running into issues. I will try to outline it as best as I can - and any help is much appreciated.

I have a form in haml that does a basic, basic form for data manipulation, search, and data column filtering.

If I try a simple datepicker like so: = form_tag (my_path, :method => :get, :controller => 'myController', :action => 'filter') do =datepicker_input :services, :serviceDate I get an error saying undefined method `serviceDate' however if I modify this a little and change :serviceDate to an existing method in my controller, it will render the datepicker, but will have the object (#Enumerator:0x00000005bf8358) displayed in the field until I click in the datepicker and select a new date.

However, this is just a simple rendetion of what I was hoping for. My actual form looks like so:

= form_tag (my_path, :method => :get, :controller => 'myController', :action => 'filter') do = datepicker_input(:services, :serviceDate, :size => 15, :minDate => "-1Y", :maxDate => "+0D", :dateFormat => "yy-mm-dd", :value => Date.current.to_s, :onchange => "form.submit()")

If I use :defaultDate instead of :value, it'll error with the undefined method. If I use value and try to set the :value by either parameter OR text, it will also error.

I hope this makes sense. Any help would be appreciated!

albertopq commented 12 years ago

Not sure to understand your problem :S

Can you try something? Try to replace the datepicker input for a simple text_field (removing all the date-related parameters, of course) and see if your input field is shown with the sent :value on it. If that fails, it has nothing to do with the jquery_datepicker. If it shows an input with the value you sent via the :value parameter, we can work together to find what's happening.

Thanks!

ltrevisanDU commented 12 years ago

First, to address the not understanding the problem -- the datepicker works just fine if and only if the parameter is somewhere in the URL. It will populate the field and it will change when a date is selected.

However, if you try to go to the page fresh, no parameters, not form submission, the entire page will error telling me there is a nil error on [] -- which seems to trace back to the parameters being blank / empty. If I completely take out the :value => params[] and leave it blank, I will get an object (the Enumerator) or if I set it with Date object, it will default to that and work.

Does that clarify the issue?

As for replacing the datepicker input with a simple text_field to check for value setting, I can already tell you that fields are getting populated since the rest of my form is populating without an issue (shows the values I sent). This is what led me to believe, potentially prematurely, it was a jquery issue (still potentially on my end, but not one I've been able to troubleshoot my config myself), and not a form submission issue.

Thanks for your response and thanks in advance for any input you can give on the matter!

albertopq commented 12 years ago

So, what are you expecting to be as default value when no parameters sent? Current date? Empty?

Thanks!

ltrevisanDU commented 12 years ago

I'd prefer empty, but current date will work as well.

albertopq commented 12 years ago

I think there is a problem with the validation of some types sent using the :value attribute. I would need to know what exact value are you sending as :value when no parameters sent in the URL (when is failing) in order to fix it.

Could you please send me your haml view by Message or print the exact value you are passing when there are not parameters in the URL?

Thanks a lot for your contribution! :)

ltrevisanDU commented 12 years ago

I'll send you mail of the pertinent parts via mail, but for people who might read this I'll post the values here as well.

Note: I set :dateFormat, :minDate, and :maxDate

Default, this works just fine.

:value => Date.current.to_s

=> 2012-03-16

using params[:service_date]

datepicker_input(:services, :service_date, :value => params[:service_date])

=> can't dup NilClass

using params[:services][:service_date]

datepicker_input(:services, :service_date, :value => params[:services][:service_date]

=> undefined method `[]' for nil:NilClass

Attempting to check for nil and setting default

datepicker_input(:services, :service_date, :value => (params[:services][:service_date] ? params[:services][:service_date] : Date.current.to_s)

=> undefined method `[]' for nil:NilClass #nothing set

=> 2012-03-01 # value passed, date is properly shown

Another attempt to check / set default

:value => (params[:service_date] ? params[:service_date] : Date.current.to_s)

=> 2012-03-16 ## in else

choose March 1, 2012

=> 2012-03-16 ## still in else

Hope that helps.

albertopq commented 12 years ago

There were 2 problems here:

1.- When you have the "undefined method `[]' for nil:NilClass" error, is when you try to use params[:services][:service_date] when params[:services] is nil. Just checking before that params[:services] exists, should fix it.

2.- Sending an empty string ("") or nil and :dateFormat params at the same time, was making the jquery_datepicker validate the format of that empty string, so it returned an "invalid date" error. I fixed that bug and now you should be able to send empty strings and nil even passing :dateFormat params.

Please, could you confirm that is working now? Thanks for your contribution!

ltrevisanDU commented 12 years ago

Very cool, I will test this later today or tomorrow.

1.- Yep, the solution you gave in the email seemed to work just fine. If you extended this into the code, I'll be sure to take that fix out to retest.

2.- I thought it was odd that it wouldn't allow a blank or nil date, nice catch - I will update in a bit to check it was fixed.

ltrevisanDU commented 12 years ago

Confirming - You can pass an empty string ("") or nil with :dateFormat and it works great!

Thanks!