Joe
Joe

Reputation: 1765

Validators message in rails 3

In Rails 3, the validators is changed: now is possible to specify all the validators for a specific field in once:

so instead to write

Rails 2.x.x style
validates_size_of :username, :within => 5..15, :message=> "username size must be between 5 and 15"

now I can write

Rails 3 style

 validates :username,  :length => { :minimum => 5, :maximum => 40 }

But if I add :messge=> "bla bla bla" in this last example (Rails 3 style) an error occur, so the question is: How to edit personal error message to the model in order to show them in the view ?

Thank you

Upvotes: 3

Views: 2072

Answers (1)

coreyward
coreyward

Reputation: 80090

When you use the shorthand validates :model method you can only add specific messages within the context of a specific validator. Example:

validates :username, :length => { :minimum => 5, :maximum => 40, :message => 'should be between 5 and 40 characters' }

Note that the message is in the hash for the :length key. Otherwise Rails doesn't know which validator the message should be applied to.

Upvotes: 1

Related Questions