ActiveAdmin 无效表单仍然提交

ActiveAdmin invalid form still submit

在 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

这个问题是由于 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