如何解决ActiveAdmin 无效表单仍然提交
在 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。