Validation message on association #717

Closed
samdebutcha opened this Issue Sep 14, 2011 · 7 comments

Comments

Projects
None yet
2 participants
@samdebutcha
Contributor

samdebutcha commented Sep 14, 2011

class User < ActiveRecord::Base
has_and_belongs_to_many :roles
validates :roles, :presence => {:message => 'cannot save without role'}
end

Trying to save User without assigning role, shows no error validation message.

@bbenezech

This comment has been minimized.

Show comment
Hide comment
@bbenezech

bbenezech Sep 15, 2011

Collaborator

I was not aware it is possible to validate presence of associateds through has_many. Does it work without RA? In console for example?

Collaborator

bbenezech commented Sep 15, 2011

I was not aware it is possible to validate presence of associateds through has_many. Does it work without RA? In console for example?

@samdebutcha

This comment has been minimized.

Show comment
Hide comment
@samdebutcha

samdebutcha Sep 15, 2011

Contributor

validates :roles, :presence => {:message => "Missing role?"}

Yes, it works in the console:

u = User.create(:userid => "5", :password => "mypasswd", :email => "xxx@xxx.com")
u.errors
ActiveModel::Errors:0x406ab40 @messages= OrderedHash
{:email2=>["съдържа невярна стойност"], :roles=>["Missing role?"]}

Contributor

samdebutcha commented Sep 15, 2011

validates :roles, :presence => {:message => "Missing role?"}

Yes, it works in the console:

u = User.create(:userid => "5", :password => "mypasswd", :email => "xxx@xxx.com")
u.errors
ActiveModel::Errors:0x406ab40 @messages= OrderedHash
{:email2=>["съдържа невярна стойност"], :roles=>["Missing role?"]}

@bbenezech

This comment has been minimized.

Show comment
Hide comment
@bbenezech

bbenezech Sep 17, 2011

Collaborator

Have a look there: https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/fields/association.rb#L84

Field::HasManyAssociation is inheriting from Field::Association and apparently using child_key for retrieving errors on its field. We should override this behavior in Field::HasManyAssociation and use association's name instead.
If it's supported by AM, then we should add a test too.

Collaborator

bbenezech commented Sep 17, 2011

Have a look there: https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/fields/association.rb#L84

Field::HasManyAssociation is inheriting from Field::Association and apparently using child_key for retrieving errors on its field. We should override this behavior in Field::HasManyAssociation and use association's name instead.
If it's supported by AM, then we should add a test too.

@samdebutcha

This comment has been minimized.

Show comment
Hide comment
@samdebutcha

samdebutcha Sep 21, 2011

Contributor

Yes, if override in Field::HasManyAssociation
def errors
bindings[:object].errors[name]
end
it shows the message: roles Missing role
Thank you. What happens now?

Contributor

samdebutcha commented Sep 21, 2011

Yes, if override in Field::HasManyAssociation
def errors
bindings[:object].errors[name]
end
it shows the message: roles Missing role
Thank you. What happens now?

@bbenezech

This comment has been minimized.

Show comment
Hide comment
@bbenezech

bbenezech Sep 22, 2011

Collaborator

Pull request? :D

Collaborator

bbenezech commented Sep 22, 2011

Pull request? :D

@samdebutcha

This comment has been minimized.

Show comment
Hide comment
@samdebutcha

samdebutcha Sep 22, 2011

Contributor

Never pulled request before, never added test before.
Some guidance?

Contributor

samdebutcha commented Sep 22, 2011

Never pulled request before, never added test before.
Some guidance?

@samdebutcha

This comment has been minimized.

Show comment
Hide comment
@samdebutcha

samdebutcha Sep 22, 2011

Contributor

OK, I pulled request.

Contributor

samdebutcha commented Sep 22, 2011

OK, I pulled request.

bbenezech added a commit that referenced this issue Sep 22, 2011

@samdebutcha samdebutcha closed this Oct 8, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment