bootstrap_form icon indicating copy to clipboard operation
bootstrap_form copied to clipboard

collection_select validation errors missing

Open elik-ru opened this issue 8 years ago • 11 comments
trafficstars

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"

elik-ru avatar Feb 20 '17 21:02 elik-ru

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?

mattbrictson avatar Feb 21 '17 16:02 mattbrictson

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.

elik-ru avatar Feb 22 '17 13:02 elik-ru

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?

mattbrictson avatar Feb 26 '17 02:02 mattbrictson

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.

elik-ru avatar Feb 26 '17 10:02 elik-ru

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!

mattbrictson avatar Feb 26 '17 16:02 mattbrictson

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.

kraflab avatar Mar 01 '17 11:03 kraflab

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.

abevoelker avatar May 24 '17 06:05 abevoelker

It looks like this was fixed with a commit, and the issue didn't get closed. Please re-open if I misunderstand.

lcreid avatar Jan 26 '18 21:01 lcreid

@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.

mike1o1 avatar Feb 09 '18 06:02 mike1o1

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.

lcreid avatar Feb 09 '18 22:02 lcreid

hi, at the moment collection_select still do not show validation errors, error_key: support #334 it's still unmerged? Thanks

wljgo avatar Jan 27 '23 11:01 wljgo