#854 ✓resolved

Cannot set custom messages for validates_within

Reported by ojak | May 16th, 2009 @ 02:05 AM | in 0.10.0

Expected behavior

  • When using validates_within, you should be able to specify :message=>"Invalid range.", or :messages=>{:less_than=>"...",:greater_than=>"...", etc.}

Actual behavior

  • Cannot set a custom message.


class Contact
   property :label, String
   validates_within :label, :set => ["one", "two"], :message=>"Invalid label"

IRB test

irb> @contact = Contact.new
irb> @contact.valid?
irb> @contact.errors
#<DataMapper::Validate::ValidationErrors:0x2569c9c @errors={:label=>["Label must be one of [one, two]"]}

Comments and changes to this ticket

  • ojak

    ojak May 16th, 2009 @ 06:02 PM

    This patch allows a single message override so that it behaves like other validators (as in previous comment's example).

  • ojak

    ojak May 16th, 2009 @ 06:08 PM

    • Assigned user set to “Dan Kubb (dkubb)”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 7th, 2009 @ 02:33 AM

    • Assigned user changed from “Dan Kubb (dkubb)” to “Michael Klishin (antares)”
  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) July 9th, 2009 @ 05:02 PM

    • State changed from “unconfirmed” to “resolved”
    • Milestone set to 0.10.0

    As can be seen below, this seems to be fixed in 0.10

    require "rubygems"
    require "dm-core"
    require "dm-validations"
    require "spec"
    DataMapper::Logger.new(STDOUT, :debug)
    DataMapper.setup(:default, "sqlite3::memory:")
    class Contact
       include DataMapper::Resource
       property :id,    Serial
       property :label, String
       validates_within :label, :set => ["one", "two"], :message=>"Invalid label"
    describe "validates_within" do
      before(:all) do
      it "should allow to overwrite the default error message" do
        contact = Contact.new
        contact.errors.on(:label).should == ["Invalid label"]
    # mungo:snippets snusnu$ spec -cfs 854.rb 
    # validates_within
    #  ~ (0.000151) SELECT sqlite_version(*)
    #  ~ (0.000108) DROP TABLE IF EXISTS "contacts"
    #  ~ (0.000027) PRAGMA table_info("contacts")
    #  ~ (0.000371) CREATE TABLE "contacts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "label" VARCHAR(50))
    # - should allow to overwrite the default error message
    # Finished in 0.005372 seconds
    # 1 example, 0 failures

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »