LoginSignup
54
52

More than 5 years have passed since last update.

RailsでBootstrapを使ったときにフォームエラーをキレイに表示する方法

Last updated at Posted at 2014-01-28

スクリーンショット 2014-01-28 13.20.06.png

ソースコード例

<div class="row">
  <div class="col-md-6">
    <h2><%= t('views.sign_up') %></h2>
    <%= form_for resource do |f| %>
      <%= f.error_messages! %>

      <div class="form-group <%= f.error_css(:nickname) %>">
        <%= f.label :nickname, class: 'control-label' %>
        <%= f.text_field :nickname, class: 'form-control' %>
      </div>

      <div class="form-group <%= f.error_css(:email) %>">
        <%= f.label :email, class: 'control-label' %>
        <%= f.email_field :email, class: 'form-control' %>
      </div>

      <div class="form-group <%= f.error_css(:password) %>">
        <%= f.label :password, class: 'control-label' %>
        <%= f.password_field :password, class: 'form-control' %>
      </div>

      <div class="form-group <%= f.error_css(:password_confirmation) %>">
        <%= f.label :password_confirmation, class: 'control-label' %>
        <%= f.password_field :password_confirmation, class: 'form-control' %>
      </div>

      <%= f.submit t('.submit'), class: 'btn btn-success btn-centered' %>
    <% end %>
  </div>
</div>

form_forf.error_messages! でまとめてエラーメッセージを表示でき、個別の項目は f.error_css(:nickname) のように表示できます。

やりかた

FormBuilder を拡張するだけ。application_helper.rb を次のようにします。

app/helpers/application_helper.rb
module ApplicationHelper
end

module ActionView
  module Helpers
    module FormHelper
      def error_messages!(object_name, options = {})
        resource = self.instance_variable_get("@#{object_name}")
        return '' if !resource || resource.errors.empty?

        messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join

        html = <<-HTML
          <div class="alert alert-danger">
            <ul>#{messages}</ul>
          </div>
        HTML

        html.html_safe
      end

      def error_css(object_name, method, options = {})
        resource = self.instance_variable_get("@#{object_name}")
        return '' if resource.errors.empty?

        resource.errors.include?(method) ? 'has-error' : ''
      end
    end

    class FormBuilder
      def error_messages!(options = {})
        @template.error_messages!(@object_name, options.merge(object: @object))
      end

      def error_css(method, options = {})
        @template.error_css(@object_name, method, options.merge(object: @object))
      end
    end
  end
end
config/application.rb
module YourApp
  class Application < Rails::Application
    #..
    config.action_view.field_error_proc = proc { |html_tag, _| html_tag }
  end
end

内容は見ての通り。拡張の仕方とか、もろもろご指摘ありましたらコメントください。。あと、gem で配布とかのやり方は勉強中なので、誰か簡単にできる方いましたら勝手に持って行っていただければと。

54
52
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
52