bootstrap_form
bootstrap_form copied to clipboard
Ability to explicitly pass in the error to be shown
I have a huge complicated form which contains with multiple subforms(in some loops) with custom namings and etc... I process data and validate each model and extract the following error structure:
@errors = {
:spec => {
:"1" => {
:section => {
:"6" => {
:errors => #<ActiveModel::Errors:0x007f0d807811c8 @base=#<BuildDetail id: nil, admin_sections_id: 6, admin_specs_id: 1, value: nil, created_at: nil, updated_at: nil, options: {"admin_stuffs"=>{"id"=>"2"}}>, @messages={:value=>["needs to be filled!"]}, @details={:value=>[{:error=>:blank}]}>
},
:"7" => {
:errors => #<ActiveModel::Errors:0x007f0d89d0cb98 @base=#<BuildDetail id: nil, admin_sections_id: 7, admin_specs_id: 1, value: nil, created_at: nil, updated_at: nil, options: {"admin_stuffs"=>{"id"=>"1"}}>, @messages={:value=>["needs to be filled!"]}, @details={:value=>[{:error=>:blank}]}>
},
:"8" => {
:errors => #<ActiveModel::Errors:0x007f0d84604968 @base=#<BuildDetail id: nil, admin_sections_id: 8, admin_specs_id: 1, value: nil, created_at: nil, updated_at: nil, options: {"admin_stuffs"=>{"id"=>"3"}}>, @messages={:value=>["needs to be filled!"]}, @details={:value=>[{:error=>:blank}]}>
}
}
},
:"2" => {
:section => {
:"6" => {
:errors => #<ActiveModel::Errors:0x007f0d80234990 @base=#<BuildDetail id: nil, admin_sections_id: 6, admin_specs_id: 2, value: nil, created_at: nil, updated_at: nil, options: {}>, @messages={:value=>["needs to be filled!"]}, @details={:value=>[{:error=>:blank}]}>
},
:"7" => {
:errors => #<ActiveModel::Errors:0x007f0d8977dc68 @base=#<BuildDetail id: nil, admin_sections_id: 7, admin_specs_id: 2, value: nil, created_at: nil, updated_at: nil, options: {}>, @messages={:value=>["needs to be filled!"]}, @details={:value=>[{:error=>:blank}]}>
},
:"8" => {
:errors => #<ActiveModel::Errors:0x007f0d8416a0d8 @base=#<BuildDetail id: nil, admin_sections_id: 8, admin_specs_id: 2, value: nil, created_at: nil, updated_at: nil, options: {}>, @messages={:value=>["needs to be filled!"]}, @details={:value=>[{:error=>:blank}]}>
}
}
}
}
}
I can say which elements are belong to which [:spec]/[:section]
but there is no documentation on how can I pass the :errors
to the tag? i.e Is there anything like the following in the api?
<%= f.text_field :foobar, error: @errors[:spec][spec_id][:section][section_id][:errors] %>
Thanks in advance.
To my knowledge there is no way to explicitly pass in errors. The form simply delegates to the errors
attribute of your model, as you can see here:
https://github.com/bootstrap-ruby/rails-bootstrap-forms/blob/ff4f7ff127d582fbe340e8ca93ec93b0e7acac02/lib/bootstrap_form/form_builder.rb#L400-L402
I see.... although I come up with a workaround for this, but how about we add a new option named error
for the tags which the user can pass the custom error(like the example above), IF no error
passed, then the BS_form consider the object
's error?
how about we add a new option named
error
for the tags which the user can pass the custom error
To be honest, this is not a pattern that I wantbootstrap_form
to encourage right now. I think using ActiveRecord's errors
attribute is the best solution for most people.
That said, I see how it could come in handy for some edge cases such as yours. If you (or another interested contributor) would like to put together a PR, I'll consider merging it if the implementation is clean and well-tested.
I think this could be a thing. Bootstrap 4 allows setting predefined invalid-feedback
on form elements for JS validation. See: https://getbootstrap.com/docs/4.0/components/forms/#validation
I makes sense to allow setting it to whatever you want. Errors that come from Rails during form render will replace default messages.