Please choose from our list of builds for Ember-Validations
You will require Ruby to be installed on your system. If it is please run the following:
gem install bundler
git clone git://github.com/dockyard/ember-validations.git
cd ember-validations
bundle install
bundle exec rake dist
The builds will be in the dist/
directory.
If it is a bug please open an issue on GitHub.
You need to mixin Ember.Validations.Mixin
into any controller you want to add
validations to:
var App.UserController = Ember.ObjectController.extend(Ember.Validations.Mixin);
You define your validations as a JSON object. They should be added to
the controller that represents the model in question.
The keys in the object should map to properties. If you pass a
JSON object as the value this will be seen as validation rules to apply
to the property. If you pass true
then the property itself will be
seen as a validatable object.
App.UserController.reopen({
validations: {
firstName: {
presence: true,
length: { minimum: 5 }
},
age: {
numericality: true
},
profile: true
}
});
The currently supported validators
Validates the property has a value that is null
, undefined
, or ''
true
- Passing justtrue
will activate validation and use default messagemessage
- Any string you wish to be the error message. Overridesi18n
.
// Examples
absence: true
absence: { message: 'must be blank' }
By default the values '1'
, 1
, and true
are the acceptable values
true
- Passing justtrue
will activate validation and use default messagemessage
- Any string you wish to be the error message. Overridesi18n
.accept
- the value for acceptance
// Examples
acceptance: true
acceptance: { message: 'you must accept', accept: 'yes' }
Expects a propertyConfirmation
to have the same value as
property
true
- Passing justtrue
will activate validation and use default messagemessage
- Any string you wish to be the error message. Overridesi18n
.
// Examples
confirmation: true
confirmation: { message: 'you must confirm' }
A list of values that are not allowed
message
- Any string you wish to be the error message. Overridesi18n
.allowBlank
- Iftrue
skips validation if value is emptyin
- An array of values that are excludedrange
- an array with the first element as the lower bound the and second element as the upper bound. Any value that falls within the range will be considered excluded
// Examples
exclusion: { in: ['Yellow', 'Black', 'Red'] }
exclusion: { range: [5, 10], allowBlank: true, message: 'cannot be between 5 and 10' }
A regular expression to test with the value
message
- Any string you wish to be the error message. Overridesi18n
.allowBlank
- Iftrue
skips validation if value is emptywith
- The regular expression to test with
// Examples
format: { with: /^([a-zA-Z]|\d)+$/, allowBlank: true, message: 'must be letters and numbers only' }
A list of the only values allowed
message
- Any string you wish to be the error message. Overridesi18n
.allowBlank
- Iftrue
skips validation if value is emptyin
- An array of values that are allowedrange
- an array with the first element as the lower bound the and second element as the upper bound. Only values that fall within the range will be considered allowed
// Examples
inclusion: { in: ['Yellow', 'Black', 'Red'] }
inclusion: { range: [5, 10], allowBlank: true, message: 'must be between 5 and 10' }
Define the lengths that are allowed
number
- Alias foris
array
- Will expand tominimum
andmaximum
. First element is the lower bound, second element is the upper bound.allowBlank
- Iftrue
skips validation if value is emptyminimum
- The minimum length of the value allowedmaximum
- The maximum length of the value allowedis
- The exact length of the value allowedtokenizer
- A function that should return a object that responds tolength
tooShort
- the message used when theminimum
validation fails. Overridesi18n
tooLong
- the message used when themaximum
validation fails. Overridesi18n
wrongLength
- the message used when theis
validation fails. Overridesi18n
// Examples
length: 5
length: [3, 5]
length: { is: 10, allowBlank: true }
length: { minimum: 3, maximum: 5, messages: { tooShort: 'should be more than 3 characters', tooLong: 'should be less than 5 characters' } }
length: { is: 5, tokenizer: function(value) { return value.split(''); } }
Will ensure the value is a number
true
- Passing justtrue
will activate validation and use default messageallowBlank
- Iftrue
skips validation if value is emptyonlyInteger
- Will only allow integersgreaterThan
- Ensures the value is greater thangreaterThanOrEqualTo
- Ensures the value is greater than or equal toequalTo
- Ensures the value is equal tolessThan
- Ensures the value is less thanlessThanOrEqualTo
- Ensures the value is less than or equal toodd
- Ensures the value is oddeven
- Ensures the value is even
greaterThan
- Message used when value failes to be greater than. Overridesi18n
greaterThanOrEqualTo
- Message used when value failes to be greater than or equal to. Overridesi18n
equalTo
- Message used when value failes to be equal to. Overridesi18n
lessThan
- Message used when value failes to be less than. Overridesi18n
lessThanOrEqualTo
- Message used when value failes to be less than or equal to. Overridesi18n
odd
- Message used when value failes to be odd. Overridesi18n
even
- Message used when value failes to be even. Overridesi18n
// Examples
numericality: true
numericality: { odd: true, messages: { odd: 'must be an odd number' } }
numericality: { onlyInteger, greaterThan: 5, lessThanOrEqualTo : 10 }
Validates the property has a value that is not null
, undefined
, or ''
true
- Passing justtrue
will activate validation and use default messagemessage
- Any string you wish to be the error message. Overridesi18n
.
// Examples
presence: true
presence: { message: 'must not be blank' }
Not yet implemented.
Validates the property has a value that is a URL.
allowBlank
- Iftrue
skips validation if value is emptyallowIp
- Passingtrue
will validate URLs using IP address. By default, IP addresses will be invalid.allowUserPass
- Passingtrue
will validate URLs with username / passwords. By default, usernames and password will be invalid.allowPort
- Iftrue
will validate URLs with ports. By default, URLs with ports will be invalid.domainOnly
- Iftrue
will only allow domains/sub-domains to be valid. URLs with protocols, ports or paths will be invalid.protocols
- An array with accepted protocols. Default protocols arehttp
andhttps
.
// Examples
url { allowUserPass: true }
url { allowBlank: true, allowIp: true, allowPort: true, protocols: ['http', 'https', 'ftp'] }
url { domainOnly: true }
Each validator can take an if
or an unless
in its options
hash.
The value of the conditional can be an inline function, a string that
represents a property on the object, or a string that represents a
function on the object. The result should be a boolean.
// function form
firstName: {
presence: {
if: function(object, validator) {
return true;
}
}
}
// string form
// if 'canValidate' is a function on the object it will be called
// if 'canValidate' is a property object.get('canValidate') will be called
firstName: {
presence: {
unless: 'canValidate'
}
}
Validations will automatically run when the object is created and when
each property changes. isValid
states bubble up and help define the
direct parent's validation state. isInvalid
is also available for convenience.
If you want to force all validations to run simply call .validate()
on the object. isValid
will be set to true
or false
. All validations are run as deferred objects, so the validations will
not be completed when validate
is done. So validate
returns a promise, call then
with a function containing the code you want to run after the validations have
completed.
user.validate().then(function() {
user.get('isValid'); // true
user.get('isInvalid'); // false
})
After mixing in Ember.Validations.Mixin
into your object it will now have a
.errors
object. All validation error messages will be placed in there
for the corresponding property. Errors messages will always be an array.
App.User = Ember.Object.extend(Ember.Validations.Mixin, {
validations: {
firstName: { presence: true }
}
});
user = App.User.create();
user.validate().then(null, function() {
user.get('isValid'); // false
user.get('errors.firstName'); // ["can't be blank"]
user.set('firstName', 'Brian');
user.validate().then(function() {
user.get('isValid'); // true
user.get('errors.firstName'); // []
})
})
When you use ember-i18n your Ember.I18n.translations
object should contain the following keys under the errors
key:
Ember.I18n.translations = {
errors:
inclusion: "is not included in the list",
exclusion: "is reserved",
invalid: "is invalid",
confirmation: "doesn't match {{attribute}}",
accepted: "must be accepted",
empty: "can't be empty",
blank: "can't be blank",
present: "must be blank",
tooLong: "is too long (maximum is {{count}} characters)",
tooShort: "is too short (minimum is {{count}} characters)",
wrongLength: "is the wrong length (should be {{count}} characters)",
notANumber: "is not a number",
notAnInteger: "must be an integer",
greaterThan: "must be greater than {{count}}",
greaterThanOrEqualTo: "must be greater than or equal to {{count}}",
equalTo: "must be equal to {{count}}",
lessThan: "must be less than {{count}}",
lessThanOrEqualTo: "must be less than or equal to {{count}}",
otherThan: "must be other than {{count}}",
odd: "must be odd",
even: "must be even"
}
We are very thankful for the many contributors
This library follows Semantic Versioning
Please do! We are always looking to improve this gem. Please see our Contribution Guidelines on how to properly submit issues and pull requests.
DockYard, LLC © 2013