Formtastic Date Fields

Showing 1-13 of 13 messages
Formtastic Date Fields Gernot 5/16/09 12:35 AM
Hi

I always thought that the 'V' part of rails is the weakest. Then I
found formtastic and I absolutely love it. The only problem for me is
the handling of date fields. I often have the need to let the user
enter dates in my forms and I think that the rails approach (3 select
dropdowns) is unacceptable for my users. Therefore I use ordinary text
boxes for dates.

Unfortunately, rails does not respect my I18n date formats and renders
the date always 'yyyy-mm-dd' in the input control. Using the rails
form helpers, however, I can override this using the value argument
like so:

=form.text_field :date_attr, :value => @current_object.date_attr ?
I18n.l(@current_object.date_attr) : ""

But this does not work with the formtastic helpers. How can I override
the date format in formtasic? Or, wouldn't it be a good idea to
support date fields as text boxes in formtastic and use the I18n date
format to display the current value in the input control?

Gernot
Re: Formtastic Date Fields Justin French 5/16/09 2:05 AM
Hi, you can definitely render the date fields as a string (or  
anythingrlse really) with the :as option:

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

I don't have the code in front of me right now, but I'm certain there  
is, or should be a way to get what you want. Let me know how you go,  
I'll take a look soon.

Justin French
Re: Formtastic Date Fields Gernot 5/16/09 2:24 AM
Hi Justin

I tried that already and it works. But when I display an already
existing record, the date is alwasy rendered in the format 'yyyy-dd-
mm' which is ugly. When I do it like this:

=form.input :attr, :as => :string, :input_html => {:value =>
@object.attr ? I18n.l(@object.attr) : ""}

I get the behaviour I want. But wouldn't it be nice if Formtastic
would recognise that I want to display a date as text and render the
input_html automatically? All that would be needed is to render the
value according to I18n format definitions. Now I have to use the
input_html statement for every date field, which is not very elegant
and DRY. Of course, I could write a helper for this but wouldn't it be
nice to bake that right into formtastic? Maybe as an option?

Gernot Kogler

On 16 Mai, 11:05, Justin French <jus...@indent.com.au> wrote:
> Hi, you can definitely render the date fields as a string (or  
> anythingrlse really) with the :as option:
>
> f.input :foo, :as => :string
>
> I don't have the code in front of me right now, but I'm certain there  
> is, or should be a way to get what you want. Let me know how you go,  
> I'll take a look soon.
>
> Justin French
>
Re: Formtastic Date Fields Justin French 5/16/09 3:47 PM
It definitely needs to be baked in.

Justin French
Re: Formtastic Date Fields Gernot 5/16/09 10:38 PM
Cool! There's one more thing: When the user enters an invalid date,
you should display the raw value before typecast so the wrong input is
displayed in the input control. Otherwise the user cannot see what he
entered. My final input_html look s like this:

=form.input :attr, :as => :string, :input_html => {:value =>
@object.attr ? I18n.l(@object.attr) : @object.attr_before_type_cast ||
""}

Gernot Kogler

On 17 Mai, 00:47, Justin French <jus...@indent.com.au> wrote:
> It definitely needs to be baked in.
>
> Justin French
>
Re: Formtastic Date Fields Justin French 5/18/09 2:28 PM
José has added this to the issue tracker, but I'm really not clear on  
the solution right now -- especially the typecasting issue, since  
we're obviously leaning on the existing Rails patterns and helpers a  
lot under the hood, and this is obviously a pretty specific and  
special case.

Off the top of my head, *maybe* we could introduce a :typecast =>  
false option?

José will be much better equipped to figure out the i18n stuff than I  
will ;)

Justin
---
Justin French
justin...@indent.com.au
http://justinfrench.com

Re: Formtastic Date Fields José Valim 5/18/09 2:41 PM
Guys,

I'm not sure on the API.

Do you really think that it should be handled as a string input? Why not a custom input? For example:

  f.input :attr, :as => :date_string

Or as a date special type:

  f.input :attr, :as => :date, :stringify => true

Just thinking out loud too!

--
José Valim
Re: Formtastic Date Fields Gernot 5/18/09 10:02 PM
Hi Justin

I'm not sure if I see it too simplified. But I think the extension
should be pretty easy, just a few lines of code. Rails does the
typecasting anyways, so there's no need to change something there.

I18n.l(attr) autmatically detects the data type and formats the value
acording to the locales definitions. So all that needs to be done is:
 - when rendering a string input, detect if the attribute is a date
 - if so, generate a custom html_input like in my example (which can
be overriden of course)

Gernot
> justin.fre...@indent.com.auhttp://justinfrench.com
Re: Formtastic Date Fields Gernot 5/18/09 10:10 PM
Ho José

To keep the formtastic dsl simple, I wouldn't introduce a new input
type or extend the arguments. Instead, I would add a new global
formtastic configuration:

Formtastic::SemanticFormBuilder.date_input = :string

I think most users will use the current or the new date option
globally in their apps (not a mix), so it makes sense to me to
configure it for the whole app.
What do you think?

Gernot
Re: Formtastic Date Fields José Valim 5/19/09 2:11 AM
Gernot,

One of the things that we were discussing previously on the mailing list is custom inputs.

Formtastic currently does not make easy to create or modify existing inputs and this is definitely something we should tackle. For example, although Formtastic date/time inputs are more readable, I always end up using Rails ones, because they already work with Cucumber. So in my apps I have this:

module Formtastic
  class SemanticFormBuilder
    [:date, :time, :datetime].each do |input|
      class_eval <<-DATETIME
      def #{input}_input(method, options)
        html_options = options.delete(:html_options) || {}
        self.label(method, options.delete(:label), options.slice(:required)) +
        self.#{input}_select(method, set_options(options), html_options)
      end
      DATETIME
    end
  end
end

And everyone is tackling the problem differently and/or having different inputs. In other applications, I have custom inputs for money (2 dollars) and so on. It would be quite hard for formtastic to handle ALL those inputs.

So right now I'm not sure wether formtastic should handle this date string case as well. You will probably use a datepicker later and other customizations and maybe formtastic won't be useful anymore.

It would be better if we release version 1.0 as soon as possible and start to work on the inputs customization with partials. So this would become a very easy problem to tackle.



--
José Valim
Re: Formtastic Date Fields Mark Mansour 5/19/09 2:58 AM
I would agree with that though Jose.  I have lots of (too much) Formtastic customization that I want to throw away once templates are included.  Go version 1!--
Mark Mansour
ma...@stateofflux.com
Re: Formtastic Date Fields Gernot 5/19/09 6:31 AM
Hi José

I can live with that :-) I've now made a patch form me that works the
way I want it:

module Formtastic
  class SemanticFormBuilder
    def date_input(method, options)
      current_value = @object.send(method)
      html_options ||= {:value =>  current_value ? I18n.l
(current_value) : @object.send("#{method}_before_type_cast")}
      self.label(method, options.delete(:label), options.slice
(:required)) +
      self.send(:text_field, method, html_options)
    end
  end
end

Gernot

On 19 Mai, 11:58, Mark Mansour <m...@stateofflux.com> wrote:
> I would agree with that though Jose.  I have lots of (too much) Formtastic
> customization that I want to throw away once templates are included.  Go
> version 1!
>
>
>
>
>
> m...@stateofflux.com
Re: Formtastic Date Fields Justin French 5/19/09 3:19 AM
I was thinking the exact same thing on the train home tonight.

This whole "problem" will go away when/if we move to partials that can  
be overridden for each input type.

You'll simply redefine how your application will handle date inputs to  
suit your needs.  In the meantime, just fork the repo and do whatever  
you need to do ;)

JF