bootstrap_form
bootstrap_form copied to clipboard
collection_select validation errors missing
Suppose a have a model
Class A < ApplicationRecord end
with attributes id and title
and a model
Class B < ApplicationRecord belongs_to :a validates_presence_of :a end
and a form
- bootstrap_form_for @b do |f| = f.collection_select :a_id, A.all, :id, :title, {prompt: "Select one}
and a code in controller @b.update(params.require(:b).premit(:a_id))
I don't see validation errors when user leaves selection blank. This is because @b.errors.messages[:a] is set, and bootstrap_forms expects @b.errors.messages[:a_id] to be set.
The same problem with habtm relation and collection_select with {multiple: true}.
If i change my code to
- bootstrap_form_for @b do |f| = f.collection_select :a, A.all, :id, :title, {prompt: "Select one}
@b.update(params.require(:b).premit(:a))
a get an error "got string, A expected"
Yep, this seems like a bug to me. However, I think it is a limitation due to Rails itself: namely, there is a mismatch in terms of the form field (:a_id) and the validation rule (:a). Do you have any ideas on how we could address this?
I think it's possible to use Rails' naming convention:
for collection_select take field name (a_id), remove "_id" part, use the rest for errors' check. for collection_select {multiple: true} take field name (a_ids), remove "_ids" part, pluralize the rest ("as") and use it for errors' check.
That would be nice in the general case, but belongs_to can be customized to use a different column name, so it would work all the time. I think there needs to be a way to handle this edge case. Perhaps an :error_key option?
As far as i know, column name does not affect ..._id method name.
Anyway a possibility to set some option like :error_key sounds helpful.
Good points. I think we have enough discussion here to help get a PR started if someone wants try fixing this. Thanks again for the detailed report!
Couldn't you use just use validates :a_id, presence: true in your model to sort this out? For generic form fields that don't match model attributes, I usually set relevant errors in the methods that handle them, since the associated errors won't necessarily match validations directly.
Ran into this issue myself so I took a stab at a PR, feel free to modify as necessary.
@mattbrictson is correct about the association being able to be totally customized (both association name and FK column), e.g.:
belongs_to :foo, class_name: "Bar", foreign_key: "baz"
so I think relying on naming conventions around "_id" could cause issues.
@kraflab - a good thought but a presence validation on an association is semantically different than one on the foreign key, because the former actually verifies that the record exists in the database (the error wording in this case is slightly different as well - "must exist" rather than "can't be blank"). For example, a user could edit a select dropdown to pass in a -1 for the _id FK field, which would pass a presence validation on the FK field but would fail on the association presence validation (because record lookup fails).
You could of course write a custom validator to do the same lookup check and set an error on the FK field, but at some point I guess you're fighting the framework, particularly since Rails 5 now by default validates presence on belongs_to.
It looks like this was fixed with a commit, and the issue didn't get closed. Please re-open if I misunderstand.
@lcreid It looks like that commit was never been merged into master. I believe this issue still exists. #334 still needs to be completed, unless I'm missing something.
Thanks @mike1o1 . It looks like you're right. As you might have seen in #361, we're pretty busy with the Bootstrap 4 version right now, but we'll keep this on our radar. And of course, any help you're willing to provide would be much appreciated.
hi, at the moment collection_select still do not show validation errors, error_key: support #334 it's still unmerged? Thanks