简体   繁体   English

如何使用Simple_form但没有模型显示验证错误消息?

[英]How to display validation error messages with Simple_form but without model?

I use Simple_form in my Rails 4 application. 我在Rails 4应用程序中使用Simple_form。

How can I display error messages in a view that is not tied to a model ? 如何在与模型无关的视图中显示错误消息?

I want to have the same result than in other views based on models. 我希望得到与基于模型的其他视图相同的结果。

For now, this is the code in the view : 现在,这是视图中的代码:

<%= simple_form_for(:registration, html: { role: 'form' }, :url => registrations_path) do |f| %>

  <%= f.error_notification %>

  <%= f.input :name, :required => true, :autofocus => true %>
  <%= f.input :email, :required => true %>
  <%= f.input :password, :required => true %>
  <%= f.input :password_confirmation, :required => true %>

  <%= f.button :submit %>

<% end %>

In a 'normal' view (ie with a model) the line <%= f.error_notification %> display errors. 在“正常”视图中(即使用模型),行<%= f.error_notification %>显示错误。

What should I do in my controller to initialize something used by Simple_form to display errors ? 我应该怎么做我的控制器初始化Simple_form使用的东西来显示错误?

Thanks 谢谢

Simple Form does not support this functionality "out of the box". Simple Form不支持​​“开箱即用”的此功能。 But you can add it with a "monkey patch" in an initializer like this (disclaimer - this appears to work for my simple test case but has not been thoroughly tested): 但你可以在这样的初始化器中添加一个“猴子补丁”(免责声明 - 这似乎适用于我的简单测试用例但尚未经过彻底测试):

// Put this code in an initializer, perhaps at the top of initializers/simple_form.rb
module SimpleForm
  module Components
    module Errors
      def has_errors?
        has_custom_error? || (object && object.respond_to?(:errors) && errors.present?)
      end

      def errors
        @errors ||= has_custom_error? ? [options[:error]] : (errors_on_attribute + errors_on_association).compact
      end
    end
  end
end

module SimpleForm
  class ErrorNotification
    def has_errors?
      @options[:errors] || (object && object.respond_to?(:errors) && errors.present?)
    end
  end
end

And then you can add errors to your form like this (note you indicate whether to show the error notification by setting 'errors: true', you would have to perform your own check to decide if there are errors present, and add the errors dynamically): 然后你可以像这样在表单中添加错误(注意你指明是否通过设置'errors:true'来显示错误通知,你必须执行自己的检查以确定是否存在错误,并动态添加错误):

=simple_form_for :your_symbol do |f|
  =f.error_notification errors: true
  =f.input :item1, as: :string, error: "This is an error on item1"
  =f.input :item2, as: :string, error: "This is an error on item2"

The simple_form_for helper must wrap a model. simple_form_for帮助程序必须包装模型。 But just because we say this doesn't mean it has to be an ActiveRecord model that's backed by a database table. 但仅仅因为我们说这并不意味着它必须是一个由数据库表支持的ActiveRecord模型。 You're free to create models that aren't backed by a database. 您可以自由创建不受数据库支持的模型。 In Rails 3+ the way to do this is to have your class include the components that you need from ActiveModel . 在Rails 3+中,执行此操作的方法是让您的类包含ActiveModel所需的组件。 This SO post explains how to do this with an example (and I'm sure there's many others out there). 这个SO帖子用一个例子解释了如何做到这一点(我确信还有很多其他的)。 Once you have a model that includes ActiveModel::Validation you can add to the errors collection and then the f.error_notification statement will output the errors as you're used to in table-backed models. 一旦你有一个包含ActiveModel::Validation的模型,你就可以添加到errors集合中,然后f.error_notification语句将输出你在习惯用于表格的模型中的错误。

TL;DR: Create a non-ActiveRecord, non-table-backed model then treat it like a regular old model and the form should do the right thing. TL; DR:创建一个非ActiveRecord,非表支持的模型,然后将其视为常规的旧模型,表单应该做正确的事情。

Use client_side_validations gem , it is simple , and you've to do only - 使用client_side_validations gem,它很简单,你只需要做 -

<%= simple_form_for(:registration, html: { role: 'form' }, :url => registrations_path) , :validate => true do |f| %> 

But you need to add validations in model also. 但是您还需要在模型中添加验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM