tower-archive / tower

UNMAINTAINED - Small components for building apps, manipulating data, and automating a distributed infrastructure.
http://tower.github.io
MIT License
1.79k stars 120 forks source link

Cannot call method 'validateEach' of undefined #347

Closed tremby closed 10 years ago

tremby commented 11 years ago

This is my first try with Tower and I'm new to frameworks like this. Apologies if this isn't the place for support requests, but I couldn't find mention of an IRC channel or message board.

I'm trying to build up my model and the first thing is an Address. I've been looking at the documentation of models and I want to use the 'inclusion' validation for street direction. I'm following what I see, and I have cut the model right down to just the street direction:

class App.Address extends Tower.Model
  @field 'streetDirection', type: 'String'
  @validates 'streetDirection', inclusion: {
    in: ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'],
    message: '%{value} is not an abbreviated compass direction',
  }

  @timestamps()

I now run tower console and try to save an empty Address to check my validation. I actually expect it to work, since I'm not saying streetDirection is required, and I'd want it to fail if I set it to, say, 'T' and then try to save. But in both cases I get this:

tower> add = App.Address.build({})
{ errors: {}, readOnly: false }
tower> add.save()
TypeError: Cannot call method 'validateEach' of undefined
    at Tower.ModelValidations.InstanceMethods.validate.runCallbacks.iterator (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-model/shared/validations.js:50:30)
    at _.extend.series.iterate (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-support/shared/shared.js:302:14)
    at _.extend.series (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-support/shared/shared.js:316:12)
    at _.extend.async (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-support/shared/shared.js:277:17)
    at Tower.ModelValidations.InstanceMethods.validate (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-model/shared/validations.js:53:15)
    at Tower.SupportCallbacks.runCallbacks (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-support/shared/callbacks.js:71:15)
    at Tower.ModelValidations.InstanceMethods.validate (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-model/shared/validations.js:39:12)
    at Tower.ModelPersistence.InstanceMethods.save (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-model/shared/persistence.js:70:21)
    at Tower.ModelTransactions.InstanceMethods.save (/home/bjn/www/mwclcdb/node_modules/tower/lib/tower-model/shared/transactions.js:22:26)
    at Ember.wrap.newFunc [as save] (/home/bjn/www/mwclcdb/node_modules/tower/node_modules/ember-metal-node/index.js:956:16)
tower> 

What am I doing wrong? My model looks just like in the inclusion part of the model documentation.

lancejpollard commented 11 years ago

Just taking a brief look at this, it looks like what you're doing is correct but this may not be fully implemented in Tower yet.

tremby commented 11 years ago

So, we have discovered that Tower doesn't accept that nested object and the message key, so it should just be

class App.Address extends Tower.Model
  @field 'streetDirection', type: 'String'
  @validates 'streetDirection'
    in: ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']

  @timestamps()

The pull request I referenced this in, come to think of it, has nothing to do with this. It was kind of a follow on point.

This should probably stay open to remind of the documentation bug, your call.