-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show error for collection on correct index #60
base: master
Are you sure you want to change the base?
Conversation
I think for Rails to return indexed errors you need to enable it either globally ... Rails.application.config.active_record.index_nested_attribute_errors = true ... or directly in the model's association ... class AlbumTracks < ActiveRecord::Base
has_many :tracks, index_errors: true
end Might be a good idea to implement it in a similar fashion here, maybe having class AlbumTracksForm < Reform::Form
collection :tracks, index_errors: true do
...
end
end ... is enough (though adding a global config does not hurt, either). |
@doits thank you for further information... and checking some reform test the current is exactly this not-indexed error doing a better research it's a feature added in rails 5, so this PR is more a discussion for a new feature, instead of a bug fix, I'm gonna update the description |
We kind of have this feature out of the box, and much cleaner than Rails 5: If you want to know a specific item's errors, you can do |
I too think that the Rails' syntax is awkward, but maybe it could be still added to be consistent with Rails. Anyway, for me it would be enough to get the errors sanely and correctly indexed, maybe like this: errors: {
title: { error: "can't be blank" } # <-- title of base model is blank
items: [ # <-- items is a collection
{}, # <-- first item has no errors
{ title: { error: "can't be blank" } } # <-- second item has some error
]
} Is this currently possible, maybe by looping over some structure manually? Is there a way to loop over every property and collection and fetch the errors? |
@doits I am pretty sure the current implementation in Reform 2.3.0.rc2 does that, you can say |
@apotonick - not if you’ve used dry validations each macro to validate your array rather than a validate block in Reform collection.
This is 100% a bug
…Sent from my iPhone
On 13 Mar 2018, at 10:38, Nick Sutterer <[email protected]<mailto:[email protected]>> wrote:
@doits<https://github.com/doits> I am pretty sure the current implementation in Reform 2.3.0.rc2 does that, you can say form.items.errors and it compiles you a list?!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#60 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ACNaJSi0GY7zbAVuCDkL0EEqcM5XFz4Gks5td6G5gaJpZM4SnZUp>.
|
I'm at |
@apotonick I just checked out master from github to have the latest verisions of I have this form: property :first_name
validates :first_name, presence: true
collection :addresses, populate_if_empty: Address do
property :city
validates :city, presence: true
end I cannot do I came up with the following though: def all_errors
base = errors.messages
base[:addresses] = addresses.map do |address|
address.errors.messages
end
base
end So {
:first_name=>["muss ausgefüllt werden"],
:"addresses.city"=>["muss ausgefüllt werden"],
:addresses=>[
{}, {:city=>["muss ausgefüllt werden"]}
]
} The But is there any way I can loop over all collection? Currently I must know that def all_errors
base = errors.messages
# base[:addresses] = addresses.map do |address|
# address.errors.messages
# end
# is there something like `all_collections` to do this?
all_collections.each do |collection_name|
base[collection_name] = send(collection_name).map do |item|
item.errors.messages
end
end
base
end |
@doits yeah, your nested_form.tracks[0].errors.must_equal {title: ['can\'t be blank']} works great @fran-worley you mean that @apotonick @fran-worley thanks for quick reply! |
UPDATE: rewrote the description once this is a feature proposal, not a fix
@doits said on gitter that reform is missing a rails 5 validation feature, index nested attribute errors (which is false by default)
When there's a collection like:
and in rails is with option enabled, instead of returning a "global" error for all elements (reform default behaviour) is like this
but when option is enabled, it should return something like:
so it's possible to know what is the invalid element