`

SimpleForm 2.0 使用方法

阅读更多
从Redmine中备份出来的,有时间再重新排版

h2. SimpleForm 2.0和 Formtastic 的整体比较

Formtastic 的主要缺点在于对HTML输出的可定制性上不够灵活。目前的系统中,想要满足各种表单的需求,就需要在每个表单页写很多重复的代码进行设置,甚至很多页面都在使用 Rails 原生的 Form Builder,这样做维护量太大。


SimpleForm 在用法上与 Formtastic 类似,同样很好的支持了I18n、表间关联、嵌套表单和表单验证等常用的功能。SimpleForm 从 2.0 开始,在可定制性上有质的突破(Twitter Bootstrap 在里边起了很关键的作用),现在的它更像是一个 Form Builder 框架,可以很方便的对它进行设置和扩展。

h2. 使用 SimpleForm 替换 Formtastic 需要做的修改

# 把 @semantic_form_for@ 替换为 @simple_form_for@
# 去掉 @<%= f.inputs do %> ... <% end %>@ 和 @<%= f.buttons do %> ... <% end %>@,替换成对应的div

h2. SimpleForm 2.0使用方法

h3. 1、核心概念:wrapper

*通常一个表单项有这些部分(SimpleForm文档中称为 component):*

# label
# input
# hint
# error
# 把上面4个 component 包含在内部的一个“容器”,通常会是一个div标签

把以上的“容器”及其内部的各个元素看成一个整体,就是一个 wrapper 了。如果把 textarea、select 这些看成是和 input 等价的东西,那么一个表单就是N个顺序排列的 wrapper 组成的。在 SimpleForm 中,对表单进行定制时,只需在配置文件中设置好N种 wrapper,然后在 _form.html.erb 中进行正常调用即可。默认情况下,一个表单中的各个表单项都使用相同的 wrapper,我们还可以对每一个表单项单独设置,以满足特殊的定制需求。在更极端的情况下,当然也可以在表单页面直接设置 HTML 标签、class 等。有了 wrapper,不管是先出前端页面,还是后端表单模板写好后需要更改,只要同样类型表单的 HTML 和 CSS 结构相对统一,就能大量的减少我们的工作量。

h3. 2、使用示例

<pre>
<%= simple_form_for @staffer, validate: true do |f| %>
  <div class="form-inputs">
  <%= f.input :username, wrapper: :inline %>
  <%= f.input :password %>
  <%= f.input :password_confirmation %>
  <%= f.input :email %>
  <%= f.input :fullname %>
  </div>

  <div class="form-actions">
  <%= f.button :submit %>
  </div>
<% end %>
</pre>

h3. 3、配置 SimpleForm

配置文件位于config/initializers/simple_form.rb,以 Bootstrap 的默认配置进行说明:
<pre><code class="ruby">
SimpleForm.setup do |config|
  # 定义一个名字为 :default 的 wrapper
  config.wrappers :default, :class => :input,
    :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
    # 输出 HTML5 标签
    b.use :html5

    # Calculates placeholders automatically from I18n
    # You can also pass a string as f.input :placeholder => "Placeholder"
    b.use :placeholder

    ## Optional extensions
    # They are disabled unless you pass `f.input EXTENSION_NAME => :lookup`
    # to the input. If so, they will retrieve the values from the model
    # if any exists. If you want to enable the lookup for any of those
    # extensions by default, you can change `b.optional` to `b.use`.

    # Calculates maxlength from length validations for string inputs
    b.optional :maxlength

    # Calculates pattern from format validations for string inputs
    b.optional :pattern

    # Calculates min and max from length validations for numeric inputs
    b.optional :min_max

    # Calculates readonly automatically from readonly attributes
    b.optional :readonly

    # :label_input 是 :label 和 :input 的打包,后边会看到它们单独出现的情况
    # label、input、hint 和 error 在表单中的顺序由他们在配置文件中定义的顺序决定
    b.use :label_input
    b.use :hint,  :wrap_with => { :tag => :span, :class => :hint }
    b.use :error, :wrap_with => { :tag => :span, :class => :error }
  end

  # 通过参数定制最外层的“容器”的标签为 div.control-group
  config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    # 这个 wrapper 方法可以嵌套,调用它可以在输出结果中增加一个 HTML 标签
    b.wrapper :tag => 'div', :class => 'controls' do |ba|
      ba.use :input
      # 定制出错信息包含在一个 span.help-inline 标签中
      ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
      # 定制提示信息包含在一个 p.help-block 标签中
      ba.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    end
  end

  config.wrappers :prepend, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |input|
      # 这里就使用了嵌套的 wrapper 以输出 2 层 div 标签
      input.wrapper :tag => 'div', :class => 'input-prepend' do |prepend|
        prepend.use :input
      end
      input.use :hint,  :wrap_with => { :tag => 'span', :class => 'help-block' }
      input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
    end
  end

  config.wrappers :append, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |input|
      input.wrapper :tag => 'div', :class => 'input-append' do |append|
        append.use :input
      end
      input.use :hint,  :wrap_with => { :tag => 'span', :class => 'help-block' }
      input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
    end
  end

  # 设置默认 wrapper
  config.default_wrapper = :bootstrap

  # 单选按钮和复选框与 label 的位置关系,:nested 是专门为 Bootstrap定制的,我们通常用 :inline
  #   :inline => input + label
  #   :nested => label > input
  config.boolean_style = :nested

  # 按钮默认的 class
  config.button_class = 'btn'

  # Default tag used for error notification helper.
  config.error_notification_tag = :div

  # CSS class to add for error notification helper.
  config.error_notification_class = 'alert alert-error'

  # 设置必填项的 label 中星号放到文字后边
  config.label_text = lambda { |label, required| "#{label} #{required}" }

  # 设置 label 的 class,默认为 nil
  config.label_class = 'control-label'

  # 表单的默认 class
  config.form_class = :simple_form

  # 禁止浏览器对表单校验(HTML5),因为客户端校验我们都使用JavaScript插件,以便各浏览器效果统一
  config.browser_validations = false

  # 表单输入框默认长度
  config.default_input_size = 20
end
</code></pre>

*因为注释中的代码看不清,我删除了很多默认设置选项,更多参数说明及用法请看:* https://github.com/plataformatec/simple_form https://github.com/plataformatec/simple_form/wiki
分享到:
评论

相关推荐

    SPRING API 2.0.CHM

    FormTag FrameworkPortlet FrameworkServlet FreeMarkerConfig FreeMarkerConfigurationFactory FreeMarkerConfigurationFactoryBean FreeMarkerConfigurer FreeMarkerTemplateUtils FreeMarkerView ...

    asp.net知识库

    ASP.NET 2.0使用Web Part创建应用程序之二(共二) 体验 .net2.0 的优雅(2) -- ASP.net 主题和皮肤 NET2.0系列介绍(一).NET 2.0 中Web 应用程序主题的切换 ASP.NET 2.0 中Web 应用程序主题的切换 2.0正式版中...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -这样所有需要占据全屏的Panel(不管你是Accordion,Panel,ContentPanel,Form,GroupPanel,SimpleForm,Tree还是Grid,TabStrip)都可以通过这种方式全屏。 -简单方便,示例可以参考 default.aspx 或者 other\...

    Axure9元件库大合集(27种)

    AxureUX交互原型Web元件库精简版v1.1.rplibAxureUX前后端Web交互原型通用元件库v2.0.rplibAxureUX数据可视化图表组件库整理.rplib Axure标准化组件库.rplib Axure高大上低保证组件库Stwo奉献.rplibDefault.rplib ...

    ExtAspNet_v2.3.2_dll

    支持的浏览器: IE 7.0+, Firefox 3.0+, Chrome 2.0+, Opera 9.5+, Safari 3.0+ 注:ExtAspNet基于一些开源的程序ExtJS, HtmlAgilityPack, Nii.JSON, YUICompressor。 示例: http://extasp.net/ 开源: ...

    Simple QQ Login v1.1(号码登录器)

    Simple QQ Login v1.1(QQ号码登录器) 发布时间:2009-4-26 开发环境:Visual Studio 2008(C# 2.0) 测试环境:XP(.NET Framework 2.0)+QQ 2008 ...刚开发 Form 不久,错误难免,只希望与各位朋友共同学习、进步!

    LiveGraph for real time graphing

    LiveGraph is a framework for real-time data...These features make LiveGraph particularly useful for all applications that require live visualisation of large amounts of data in form of graphs and charts.

    emic-element:带有vue 2.0的Web的简单UI小部件

    A simple UI widget for web with vue 2.0 风格指南 组件 base Icon 图标 Grid 栅格 form Button 按钮 Input 输入框 Radio 单选框 Checkbox 复选框 Switch 开关 Select 选择框 Update 上传 Cropper 裁剪 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    form标签 13.9.3. input标签 13.9.4. checkbox标签 13.9.5. radiobutton标签 13.9.6. password标签 13.9.7. select标签 13.9.8. option标签 13.9.9. options标签 13.9.10. textarea标签 13.9.11. hidden标签 13.9....

    ember-simple-auth-oauth2

    这是Ember Simple Auth库的扩展,该库提供了与OAuth 2.0兼容的身份验证器和授权者。 在Ember.js应用程序和OAuth 2.0服务器之间交换用户的凭据以及令牌时,您必须确保该连接使用HTTPS! 认证者 身份验证器(请参阅...

    simple-vue-validator-一个简单而灵活的vue.js验证器库。-Vue.js开发

    Simple Vue验证程序Simple Vue验证程序是Vue.js 2.0的轻量级但灵活的插件,可让您验证输入字段,并显示er Simple Vue验证器Simple Vue验证程序是Vue.js 2.0的轻量级但又灵活的插件,可让您验证输入字段,并显示错误...

    Ext3.1 21款精美主题和动态换皮肤

    this.cboTheme = new Ext.form.ComboBox({ id: 'ux-startcombo-theme', store: themestore, valueField: 'File', displayField: 'Name', mode: 'local', typeAhead: true, editable: false, triggerAction:...

    dx修复工具

    There are two types of form styles, which are normal style and simple style respectively. Normal style is the default style, and it is suitable for most users. Simple style is a simplified form, and ...

    asp.net面试题

    C# 2.0 版引入了匿名方法的概念,此类方法允许将代码块作为参数传递,以代替单独定义的方法。 8.C#中的接口和类有什么异同。 答:这个异同可多了,要说清楚还真不容易. 9.。net中读写数据库需要用到哪些类?他们的...

    usb转网卡AX88772原理图

    The AX88772B Low-power USB 2.0 to 10/100M Fast Ethernet controller is a high performance and highly integrated ASIC which enables low cost, small form factor, and simple plug-and-play Fast Ethernet ...

    Fckeditor2.6.3

    下面结合一个ASP.NET2.0的项目来具体看看FCKeditor的安装、配置、使用。在开始之前请先下载FCKeditor文件包和FCKeditor.Net 服务器控件。启用VWD2005新建一个C#的WEB Site工程,取名FCKPro。 FCKeditor安装: 所谓...

    Dreamweaver CS4 黄金插件10-02

    1. A Simple Poll V2.0.0 For Adobe Dreamweaver 2. Abrir Janela Pop-Up V1.0.2 (Portuguese) For Adobe Dreamweaver 3. Add Gradient Background Style V1.0.0 For Adobe Dreamweaver 4. Add onmouseout V1.0.0 ...

    Dreamweaver CS4 黄金插件10-05

    1. A Simple Poll V2.0.0 For Adobe Dreamweaver 2. Abrir Janela Pop-Up V1.0.2 (Portuguese) For Adobe Dreamweaver 3. Add Gradient Background Style V1.0.0 For Adobe Dreamweaver 4. Add onmouseout V1.0.0 ...

    Dreamweaver CS4 黄金插件10-1

    1. A Simple Poll V2.0.0 For Adobe Dreamweaver 2. Abrir Janela Pop-Up V1.0.2 (Portuguese) For Adobe Dreamweaver 3. Add Gradient Background Style V1.0.0 For Adobe Dreamweaver 4. Add onmouseout V1.0.0 ...

    Dreamweaver CS4 黄金插件10-03

    1. A Simple Poll V2.0.0 For Adobe Dreamweaver 2. Abrir Janela Pop-Up V1.0.2 (Portuguese) For Adobe Dreamweaver 3. Add Gradient Background Style V1.0.0 For Adobe Dreamweaver 4. Add onmouseout V1.0.0 ...

Global site tag (gtag.js) - Google Analytics