how to produce a text input field for a datetime column type

1,800 views
Skip to first unread message

Andy S

unread,
May 22, 2011, 3:39:04 AM5/22/11
to SimpleForm
I'd like to customize simple form to produce a text input field for a
datetime column type instead of multiple option fields
(f.input :start, :as => :datetime). I'am currently doing this with
Formtastic, but I'd like to transition my forms to simple form. In
Formtastic I am using jquery timepicker to select the datetime for the
text input field.

This is the html code I generate for a form input field of the
datetime column type in Formtastic:

<li class="datetime required" id="event_start_input"><label
for="event_start">Start<abbr title="required">*</abbr></label><input
id="event_start" name="event[start]" type="text" value="May 28, 2011
11:41 tt" /><img src="/images/calendar.gif" /></li>

I'd like to generate something similar to this with simple form.

From the simple form README it seems to me like custom inputs is the
way to do this, but I am not shore how. Any advice on how I can
override the default and produce a text input field for datetime is
greatly appreciated.

Best,
Andy

Carlos Antonio da Silva

unread,
May 22, 2011, 10:44:15 AM5/22/11
to plataformate...@googlegroups.com
Well, you can use the input helper + :as option as string, like this:

  f.input :start, :as => :string

This would give you a pretty similar output. 
If you want *all* your datetime inputs to be used as date/time, you have some options:

* Create a CustomFormBuilder and re-map the date / time / datetime options to string, by calling map_type (there is a section about custom form builders in the README): https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/form_builder.rb#L12
  
  map_type :date, :time, :datetime, :to => SimpleForm::Inputs::StringInput

* Create a custom input inside your app/inputs folder, and inheriting them from StringInput:

  class DateTimeInput < SimpleForm::Inputs::String
  end

* Or, you can re-map the date time input in SimpleForm itself, although I think that should be the last option =). You would probably need to call sth like this in an initializer:

  SimpleForm::FormBuilder.map_type :date, :time, :datetime, :to => SimpleForm::Inputs::StringInput

This should override SimpleForm default behavior.

Please let me know if any of these works for you.
--
At.
Carlos A. da Silva

Rafael Mendonça França

unread,
May 22, 2011, 1:49:51 PM5/22/11
to plataformate...@googlegroups.com
The second option is the best way to do this. But if you do it like Carlos said you will change the behavior of all datatime inputs.

I recommend that you create a new input type and use it in the :as options.

Eg.:

# app/inputs
class TimepickerInput < SimpleForm::Inputs::String
end

f.input :start, :as => :timepicker

-- 
Rafael Mendonça França

Andreas Saebjoernsen

unread,
May 22, 2011, 2:33:56 PM5/22/11
to plataformate...@googlegroups.com
Because of it's flexibility I'd like to create a custom input in the app/inputs folder, but I am getting a 'could not fine method' error when following one of the custom input examples in the README.  I tracked the error down to a call to a super method I think exists in the 'SimpleForm::Inputs::StringInput' class, so I am not sure why it fails so any help debugging this is appreciated.

I am using rails 3.0.7 and Ruby 1.9.2.

To simplify debugging I reproduced the error with the 'CurrencyInput' example from the README on 'https://github.com/plataformatec/simple_form'. 
# app/inputs/currency_input.rb
class CurrencyInput < SimpleForm::Inputs::StringInput
  def input
   "$ #{super}".html_safe
  end
end

The only thing I've done is to create this class in order to get a new custom input ':as => :currency'. So if I am missing a step please let me know.

After creating an 'app/inputs/currency_input.rb' according to the README and using this custom input:
           = f.input :cost, :as => :currency, :hint => "Example: 25"
I am getting the following error:
           Could not find method for :currency

The trace ends at:
app/inputs/currency_input.rb:4:in `input'
Where there is a call to super:
      "$ #{super}".html_safe
If I remove the call to super there is not error, but of cause also no html output. 

I looked at the simple_form code and the input method seems to be defined in 'lib/simple_form/inputs/string_input.rb'. 

Please let me know if you can think of a way to fix this problem.

Best,
Andy
--
Andy S., Founder
FounderLY
For those who dare
Twitter: @FounderLY 

Andy S

unread,
May 22, 2011, 3:05:33 PM5/22/11
to SimpleForm
Rafael,

I am getting a similar 'Could not find method for :timepicker' error
for the input method when declaring the custom input class:
class TimepickerInput < SimpleForm::Inputs::StringInput # Assume
you meant the StringInput class, not String
end

Seems like ruby can't find the input method in the
SimpleForm::Inputs::StringInput class. In case it is useful I put my
whole simple_form below.

Please let me know if you have any tips on what the cause can be to
this error.

Btw, if I remove the calls to the custom input methods the form works
fine.

Best,
Andy

MY FORM:
= simple_form_for @event do |f|
= f.input :title, :as => :string, :required => true, :hint =>
"Example: Amazing French cuisine."
= f.input :details, :as => :text, :hint => "Example: We will
start with Crepes and then have Souffle for dessert."
/= f.input :cost, :as => :currency, :hint => "Example: 25"
= f.input :max_seats, :label => "How many seats?", :as
=> :numeric, :hint => "Example: 5"
= f.input :end, :as => :timepicker
= f.input :address, :as => :string, :hint => "Input the
address of the venue here."

#demo4_map{:style => "width:400px;height:300px;"}
= f.input :lat, :as => :hidden
= f.input :lng, :as => :hidden

= f.button :submit


On May 22, 10:49 am, Rafael Mendonça França <rafaelmfra...@gmail.com>
wrote:
> The second option is the best way to do this. But if you do it like Carlos said you will change the behavior of all datatime inputs.
>
> I recommend that you create a new input type and use it in the :as options.
>
> Eg.:
>
> # app/inputs
> class TimepickerInput < SimpleForm::Inputs::String
> end
>
> f.input :start, :as => :timepicker
> --
> Rafael Mendonça França
> Software developer at Plataformatechttp://twitter.com/rafaelfrancahttps://github.com/rafaelfranca
> Sent with Sparrow
>
> On Sunday, 22 May, 2011 at 11:44, Carlos Antonio da Silva wrote:
>
>
>
> > Well, you can use the input helper + :as option as string, like this:
>
> >  f.input :start, :as => :string
>
> > This would give you a pretty similar output.
> > If you want *all* your datetime inputs to be used as date/time, you have some options:
>
> > * Create a CustomFormBuilder and re-map the date / time / datetime options to string, by calling map_type (there is a section about custom form builders in the README):https://github.com/plataformatec/simple_form/blob/master/lib/simple_f...
>
> >  map_type :date, :time, :datetime, :to => SimpleForm::Inputs::StringInput
>
> > * Create a custom input inside your app/inputs folder, and inheriting them from StringInput:
>
> >  class DateTimeInput < SimpleForm::Inputs::String
> >  end
>
> > * Or, you can re-map the date time input in SimpleForm itself, although I think that should be the last option =). You would probably need to call sth like this in an initializer:
>
> >  SimpleForm::FormBuilder.map_type :date, :time, :datetime, :to => SimpleForm::Inputs::StringInput
>
> > This should override SimpleForm default behavior.
>
> >  Please let me know if any of these works for you.
>

Rafael Mendonça França

unread,
May 22, 2011, 3:16:44 PM5/22/11
to plataformate...@googlegroups.com
Hi Andy,

I confirmed that there is a bug on simple_form. I'm creating an issue and I will report you when it is fixed.

-- 
Rafael Mendonça França
Reply all
Reply to author
Forward
0 new messages