【问题标题】:Simple_form not validating inputSimple_form 不验证输入
【发布时间】:2016-06-30 00:43:33
【问题描述】:

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

【问题讨论】:

    标签: html ruby-on-rails ruby simple-form


    【解决方案1】:

    来自 simple_form 文档:

    默认情况下,所有输入都是必需的。当表单对象存在时 附加到其字段的验证,简单表单告诉需要和 可选字段分开。出于性能原因,此检测是 跳过使用条件选项的验证,例如 :if 和 :unless。

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

    【讨论】:

    • 非常感谢,我刚刚从表单中删除了所需的属性,并将验证添加到模型中,它可以工作了!当用户点击提交按钮,但没有填写所有必填字段时,有什么方法会弹出错误提示“您需要填写所有必填字段”?
    • 很高兴它成功了。 rails 验证文档可能有您正在寻找的答案——查看:message (guides.rubyonrails.org/active_record_validations.html#message) 上的部分
    • 谢谢。另外,我刚刚遇到了一个问题,当我提交填写了所有输入的表单时,我收到了这个错误Missing template items/New, application/New with {:locale=&gt;[:en], :formats=&gt;[:html], :variants=&gt;[], :handlers=&gt;[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. 它告诉我模板丢失了,尽管过去当我填写表单时它只是将我重定向到我刚刚创建的项目
    • 使用 new 而不是 build 您的新操作可能需要探索(相关更改和与用户的关联到位)。
    • 不用担心,我只是想出了原因。感谢大家的帮助!
    【解决方案2】:

    对于那些寻求简单的浏览器验证的人,如果未填写必填字段,则阻止用户提交。默认情况下,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
    
    

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多