我有一个rails应用程序,用户提交表单来创建项目,然后重定向到他们刚刚创建的项目。
我遇到的问题是,如果所有字段都留空,或者甚至其中一个必填字段为空,那么数据仍然会被传递并创建一个新项目。
我已经使用required: true
属性认为这将确保输入数据,然后没有输入数据,将发生错误。虽然这不起作用。
在config/initializaers/simple_form.rb
文件中,config.browser_validations = true
设置为true。
有人知道输入仍在传递的原因吗?
简单形式:
<%= simple_form_for @item do |f| %>
<%= f.collection_select :category_id, Category.all, :id, :name, {promt: "Choose a category" }, input_html: { class: " dropdown-toggle" } %>
<%= f.input :name, label: "Your Name", required: true, error: 'Your name is required', input_html: { class: "form-control", maxlength: 30} %>
<%= f.input :title, label: "Item Title", required: true, error: 'Item title is required', input_html: { class: "form-control", maxlength: 50 } %>
<%= f.input :used?, as: :check_boxes, required: true, label: "Is Your Item Used?" %>
<%= f.input :price, label: "Item Price", required: true, error: 'Price is required', input_html: { class: "form-control", :placeholder => "$" } %>
<%= f.input :description, label: "Item Description", input_html: { class: "form-control" } %>
<%= f.input :email, label: "Email", required: true, error: 'Email is required', input_html: { class: "form-control", :placeholder => "user@email.com" } %>
<%= f.input :phone, label: "Phone Number", input_html: { class: "form-control", :placeholder => "+61 --- --- ---", :value => "+61 " } %>
<%= f.input :suburb, label: "Suburb", required: true, error: 'Suburb is required', input_html: { class: "form-control" } %>
<%= f.input :image, label: "Upload An Image (Must be less than 2mb)" %>
<%= f.button :submit %>
<% end %>
物品型号:
class Item < ActiveRecord::Base
belongs_to :category
belongs_to :user
end
物品管制员:
class ItemsController < ApplicationController
before_action :find_item, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, except: [:index, :show]
def show
end
def new
@item = current_user.items.build
end
def create
@item = current_user.items.build(items_params)
if @item.save
redirect_to @item
else
render "New"
end
end
def edit
end
private
def items_params
params.require(:item).permit(:name, :title, :price, :description, :used?, :email, :phone, :suburb, :category_id, :image, :search)
end
def find_item
@item = Item.find(params[:id])
end
end
答案 0 :(得分:1)
来自simple_form文档:
默认情况下,所有输入都是必需的。当表单对象存在时 附加到其字段的验证,简单表单告诉必需和 可选字段分开。出于性能原因,这种检测是 跳过使用条件选项的验证,例如 :if和:除非。
您可以为商品模型中的状态添加验证,例如validates :name, presence: true
您不需要以这种方式为每个输入手动包含消息。
答案 1 :(得分:0)
对于那些正在寻找简单的浏览器验证的人,如果未填写必填字段,则阻止用户提交。默认情况下,simple_form 关闭浏览器验证。
可以在配置文件中开启,如果没有配置文件,可以使用以下命令生成:
rails generate simple_form:install
config > 初始值设定项 > simple_form
# Tell browsers whether to use the native HTML5 validations (novalidate form option).
# These validations are enabled in SimpleForm's internal config but disabled by default
# in this configuration, which is recommended due to some quirks from different browsers.
# To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
# change this configuration to true.
config.browser_validations = true