Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我怎样才能加上一句;“错误”;使用simple_form将类设置为输入错误?_Ruby On Rails_Simple Form - Fatal编程技术网

Ruby on rails 我怎样才能加上一句;“错误”;使用simple_form将类设置为输入错误?

Ruby on rails 我怎样才能加上一句;“错误”;使用simple_form将类设置为输入错误?,ruby-on-rails,simple-form,Ruby On Rails,Simple Form,当呈现表单时,我需要向input/textarea/etc添加一个类,并且该字段有错误 <input type="text" class="custom-error-class" /> 是否有一种简单的方法可以附加到SimpleForm的CSS类列表中,但仅当字段的对应对象出错时才附加?您可以使用选项: 我也有同样的问题。我的解决方案是: 我创建了一个新类StringInput(它覆盖了原来的类),并从rdoc中复制了实现。我修补了该代码以检查字段本身是否有错误,如果有,则添加

当呈现表单时,我需要向input/textarea/etc添加一个类,并且该字段有错误

<input type="text" class="custom-error-class" />

是否有一种简单的方法可以附加到SimpleForm的CSS类列表中,但仅当字段的对应对象出错时才附加?

您可以使用选项:


我也有同样的问题。我的解决方案是:

我创建了一个新类StringInput(它覆盖了原来的类),并从rdoc中复制了实现。我修补了该代码以检查字段本身是否有错误,如果有,则添加一个无效类

因为我想使用包装器选项,所以我在初始值设定项中添加了一个error_类属性

完整代码:

app/inputs/string_input.rb

class StringInput < SimpleForm::Inputs::StringInput
  def input(wrapper_options = nil)
    unless string?
      input_html_classes.unshift("string")
      input_html_options[:type] ||= input_type if html5?
    end

    input_html_classes << wrapper_options[:error_class] if has_errors?
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

    @builder.text_field(attribute_name, merged_input_options)
  end
end

这会将定义的错误类添加到所有字符串输入中。

简单表单会将带有错误的
字段类添加到包装器元素中。您可以使用此选项使您的输入看起来不同:

.field_with_errors input { ... }
谢谢@vince-v

使用您的信息,我提出了这项正在进行的工作,用于将error类应用于所有类型的输入,包括配置了error_类的标签

# lib/inputs/base.rb
module SimpleForm
  module Inputs
    class Base
      def merge_wrapper_options(options, wrapper_options)
        working_wrapper_options = wrapper_options.dup

        if working_wrapper_options
          if working_wrapper_options[:error_class] && has_errors?
            working_wrapper_options[:class] =
              [working_wrapper_options[:class]] + \
              [working_wrapper_options[:error_class]]
          end
          working_wrapper_options.delete(:error_class)

          working_wrapper_options.merge(options) do |key, oldval, newval|
            case key.to_s
            when "class"
              Array(oldval) + Array(newval)
            when "data", "aria"
              oldval.merge(newval)
            else
              newval
            end
          end
        else
          options.dup
        end
      end
    end
  end
end

# config/initializers/simple_form.rb
require 'inputs/base.rb

我的解决方案是创建具有以下内容的初始值设定项:

inputs = %w[
  CollectionSelectInput
  DateTimeInput
  FileInput
  GroupedCollectionSelectInput
  NumericInput
  PasswordInput
  RangeInput
  StringInput
  TextInput
]

inputs.each do |input_type|
  superclass = "SimpleForm::Inputs::#{input_type}".constantize

  new_class = Class.new(superclass) do
    def input_html_classes
      if has_errors?
        super.push('form-control-danger')
      else
        super
      end
    end
  end

  Object.const_set(input_type, new_class)
end

现在使用simple_form 3.5.0更容易了

通过创建具有相同名称的新类()重新定义现有输入(即
StringInput
)。然后重写
input\u html\u类
方法:

# app/inputs/string_input.rb
class StringInput < SimpleForm::Inputs::StringInput
  def input_html_classes
    has_errors? ? super.push('custom-error-class') : super
  end
end
#app/inputs/string_input.rb
类StringInput
这很接近,但它在同级错误范围上添加了错误类,而不是输入本身。谢谢!!你帮我省了很多工作,这正是我想做的(我不知道他们为什么否决了你的答案。但这是唯一一种以开箱即用的方式更新输入错误样式的真正方法。让我困惑的是,人们会专门为这种情况编写一个全新的输入类。这应该是正确的答案!它在SimpleForm文档中没有很好的解释,但它是有效的,而且它是。。。简单。
inputs = %w[
  CollectionSelectInput
  DateTimeInput
  FileInput
  GroupedCollectionSelectInput
  NumericInput
  PasswordInput
  RangeInput
  StringInput
  TextInput
]

inputs.each do |input_type|
  superclass = "SimpleForm::Inputs::#{input_type}".constantize

  new_class = Class.new(superclass) do
    def input_html_classes
      if has_errors?
        super.push('form-control-danger')
      else
        super
      end
    end
  end

  Object.const_set(input_type, new_class)
end
# app/inputs/string_input.rb
class StringInput < SimpleForm::Inputs::StringInput
  def input_html_classes
    has_errors? ? super.push('custom-error-class') : super
  end
end