LoginSignup
0
0

More than 3 years have passed since last update.

simple_formのエラーメッセージを表示できるようにする

Last updated at Posted at 2019-12-26

はじめに

bootstrapでsimple_formを使っているのですがエラーメッセージを表示させる際にてこずったのでその記録を載せます

simple_form_bootstrap.rb

やったこと

スクリーンショット 2019-10-31 16.43.07.png

上のように生年月日が未入力の際にエラーメッセージを表示させようとしたのですが表示されませんでした。
localをいじってもうまくいかなかったので調べてみるとどうやらbootstrapのinitializerに変更を加えるのが正しいようです。

config/initializers/simple_form_bootstrap.rb
  config.wrappers :date_picker, tag: :div, class: "form-group", error_class: "has-error" do |b|
    b.use :html5
    b.use :placeholder
    b.use :label, class: "control-label"

    b.wrapper tag: :div do |ba|
      ba.wrapper tag: :div, class: "input-group" do |append|
        append.use :input, class: "form-control datetimepicker", data: { date_format: "YYYY/MM/DD" }
      end
      ba.use :full_error, wrap_with: { tag: "span", class: "help-block" }
      ba.use :hint, wrap_with: { tag: "p", class: "help-block" }
    end
  end
ba.use :error, wrap_with: { tag: "span", class: "help-block" }

僕の場合生年月日を入力する際、date_pickerを使っていたのでその部分の
errorをfull_errorに書き換えることで解決しました。

スクリーンショット 2019-10-31 16.51.04.png

0
0
0

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
0
0