Custom messages append instead of override #19

Open
solnic opened this Issue May 17, 2011 · 2 comments

Projects

None yet

1 participant

@solnic
DataMapper member

When you declare a property twice, and specify a custom message the second time, it gets appended to the default English message, instead of replacing it.

# In the dm-tags gem
class Tag
  property :name [...]
end

# In my user model
class Tag
  property :name, String, :required => true, :unique => true, :length => 50,
           :messages => { :length => "Le nom est trop long (50 caractères max)." }
end

# Result when validation fails (:name too long)
=> "Name must be at most 50 characters longLe nom est trop long (50 caractères max)."

Created by lwouis - 2010-09-18 16:45:35 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/1415

@solnic
DataMapper member

I can confirm that the default behavior is to append to the array of messages for the given property. I have to explicitly say that it’s actually not appending to a String tho, but it adds a new String to the array of error messages.

It seems like currently dm-validations keeps track of a single error messages array for every property. This is so that it’s possible to add multiple error messages by defining multiple validations on the same property. However, it seems that there’s currently no way to overwrite any previously defined error message, since all it does is append atm.

I wonder if it’s feasible to either

a)
make sure that every property option (like :length) can only have one associated error message

b)
keep an array for every property option, and append to that by making the error message a String, or overwrite it by giving an array?

property :name, String, :messages => { :length => "blabla"   } # append a new message
property :name, String, :messages => { :length => ["blabla"] } # replace previous messages with this

In general, we should maybe also discuss what it means to "overwrite" a property? I’m not sure wether I’m leaning towards allowing to refine it vs. simply overwriting it.

What do you guys think?

by Martin Gamsjaeger (snusnu)

@solnic
DataMapper member

Any thoughts on this?

by Martin Gamsjaeger (snusnu)

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