【问题标题】:ActiveAdmin invalid form still submitActiveAdmin 无效表单仍然提交
【发布时间】:2021-03-26 13:10:47
【问题描述】:

在 ActiveAdmin 中,我有一个包含输入和必填字段的表单,即使我将必填字段留空或在电子邮件输入的输入中输入数字,即使表单无效,我仍然可以执行提交。

我的代码:

ActiveAdmin.register User do
  actions :all, except: [:destroy, :edit]

  permit_params :email

  form do |f|
    f.semantic_errors
    f.inputs 'Invite a new user' do
      f.input :email, required: true, input_html: {required: true, type: "email" }, as: :email
      f.input :first_name
      f.input :last_name
    end
    f.actions
  end

【问题讨论】:

    标签: ruby-on-rails activeadmin formtastic


    【解决方案1】:

    这个问题是由于formtastic在所有表单上都默认设置了“novalidate”:

    https://github.com/formtastic/formtastic/issues/1001

    解决方法是创建一个初始化程序并在对您有意义的地方更新默认的 formtastic 配置,例如:

    # config/initializers/formtastic.rb
    # Formtastic is the form builder used by ActiveAdmin
    
    # You can find the original config file here:
    # https://github.com/formtastic/formtastic/blob/master/lib/generators/templates/formtastic.rb
    
    # You can opt-in to Formtastic's use of the HTML5 `required` attribute on `<input>`, `<select>`
    # and `<textarea>` tags by setting this to true (defaults to false).
    Formtastic::FormBuilder.use_required_attribute = false
    
    # You can opt-in to new HTML5 browser validations (for things like email and url inputs) by setting
    # this to true. Doing so will omit the `novalidate` attribute from the `<form>` tag.
    # See http://diveintohtml5.org/forms.html#validation for more info.
    Formtastic::FormBuilder.perform_browser_validations = true
    

    【讨论】:

    • 帮我解决了一个烦人的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2014-11-13
    • 1970-01-01
    相关资源
    最近更新 更多