无法使用 SimpleForm 为某些(文本、布尔值等)类型创建自定义输入

发布于 2024-12-04 23:09:06 字数 2672 浏览 1 评论 0原文

我不明白为什么这不能正常工作 - 或者 - 我错过了一些重要的东西?

以下是 simple_form /lib/simple_form/form_builder.rb 中的映射类型列表:

map_type :text,                                :to => SimpleForm::Inputs::TextInput
map_type :file,                                :to => SimpleForm::Inputs::FileInput
map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :password,                            :to => SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float,           :to => SimpleForm::Inputs::NumericInput
map_type :range,                               :to => SimpleForm::Inputs::RangeInput
map_type :select, :radio, :check_boxes,        :to => SimpleForm::Inputs::CollectionInput
map_type :date, :time, :datetime,              :to => SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone,                 :to => SimpleForm::Inputs::PriorityInput
map_type :boolean,                             :to => SimpleForm::Inputs::BooleanInput

第一个问题,我可以将 StringInput 类扩展为:

#app/inputs/string_input.rb
class StringInput < SimpleForm::Inputs::StringInput
  def input
    if @builder.show?
      content_tag(:p, @builder.object[attribute_name], :class => :show)
    else
      super
    end
  end
end

(我已经使用 show? 方法扩展了构建器检测上下文:action == 'show')

这在大多数情况下都有效,但在 :as => 时则无效。 :string 存在:

<%= f.input :estimated_duration_rendered, 
  :as => :string,
  :label => mt(:estimated_duration), 
  :hint => mt(:estimated_duration_hint),
  :error => false,
  :required => false,
  :input_html => { :class => :digits_11, :placeholder => mt(:estimated_duration_placeholder), :value => format_duration(resource.estimated_duration, true) }
%>

第二个问题,我可以为 StringInput、DateTimeInput、CollectionInput 和 BooleanInput 创建自定义输入,但所有其他输入都不起作用。例如:

#app/inputs/text_input.rb
class TextInput < SimpleForm::Inputs::TextInput
  def input
    if @builder.show?
      "I will never show..."
    else
      super
    end
  end
end

即使我的表单中有这个助手:

<%= f.input :description, 
            :label => mt(:description), 
            :hint => mt(:description_hint, :max => MyModel::DESC_MAX_LENGTH), 
            :input_html => { :class => :large, :rows => 8, :cols => 1, :maxlength => MyModel::DESC_MAX_LENGTH, :placeholder => mt(:description_placeholder) }, 
            :error => false
%>

当然,描述有一个文本数据类型。

我做错了什么?

谢谢。

来回

I can't figure out why this is not working as it should - or - I'm missing something important ?

Here's the list of the mapped types from simple_form / lib / simple_form / form_builder.rb:

map_type :text,                                :to => SimpleForm::Inputs::TextInput
map_type :file,                                :to => SimpleForm::Inputs::FileInput
map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :password,                            :to => SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float,           :to => SimpleForm::Inputs::NumericInput
map_type :range,                               :to => SimpleForm::Inputs::RangeInput
map_type :select, :radio, :check_boxes,        :to => SimpleForm::Inputs::CollectionInput
map_type :date, :time, :datetime,              :to => SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone,                 :to => SimpleForm::Inputs::PriorityInput
map_type :boolean,                             :to => SimpleForm::Inputs::BooleanInput

First problem, I can extend StringInput class as:

#app/inputs/string_input.rb
class StringInput < SimpleForm::Inputs::StringInput
  def input
    if @builder.show?
      content_tag(:p, @builder.object[attribute_name], :class => :show)
    else
      super
    end
  end
end

(I've extended the builder with a show? method to detect the context: action == 'show')

This is working most of the time but not when :as => :string is present :

<%= f.input :estimated_duration_rendered, 
  :as => :string,
  :label => mt(:estimated_duration), 
  :hint => mt(:estimated_duration_hint),
  :error => false,
  :required => false,
  :input_html => { :class => :digits_11, :placeholder => mt(:estimated_duration_placeholder), :value => format_duration(resource.estimated_duration, true) }
%>

Second problem, I can create custom inputs for StringInput, DateTimeInput, CollectionInput and BooleanInput but all the others are not working. For example:

#app/inputs/text_input.rb
class TextInput < SimpleForm::Inputs::TextInput
  def input
    if @builder.show?
      "I will never show..."
    else
      super
    end
  end
end

Even if I have this helper in my form:

<%= f.input :description, 
            :label => mt(:description), 
            :hint => mt(:description_hint, :max => MyModel::DESC_MAX_LENGTH), 
            :input_html => { :class => :large, :rows => 8, :cols => 1, :maxlength => MyModel::DESC_MAX_LENGTH, :placeholder => mt(:description_placeholder) }, 
            :error => false
%>

Of course, description has a text data type.

What Am I doing wrong ?

Thank you.

Fro

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-12-11 23:09:06

那么,要使用此功能,您只需要 1.5.1 版本的 SimpleForm。 :-)

Well, to use this functionality you just need the 1.5.1 version of SimpleForm. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文