Simple_form 未验证输入

Simple_form not validating input

我有一个 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

来自 simple_form 文档:

By default all inputs are required. When the form object has presence validations attached to its fields, Simple Form tells required and optional fields apart. For performance reasons, this detection is skipped on validations that make use of conditional options, such as :if and :unless.

您可以在 Item 模型中添加存在验证,例如validates :name, presence: true 您将不需要以这种方式为每个输入手动包含消息。

对于那些寻找简单的浏览器验证的人来说,如果未填写必填字段,它会阻止用户提交。默认情况下 simple_form 关闭浏览器验证。

可以在配置文件中开启,如果没有配置文件,可以通过以下命令生成:

rails generate simple_form:install

配置 > 初始值设定项 > 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