Skip to content

Instantly share code, notes, and snippets.

@dschmidt
Created October 30, 2013 22:31
Show Gist options
  • Save dschmidt/7241498 to your computer and use it in GitHub Desktop.
Save dschmidt/7241498 to your computer and use it in GitHub Desktop.
easyform and validation builds
// ==========================================================================
// Project: Ember EasyForm
// Copyright: Copyright 2013 DockYard, LLC. and contributors.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
// Version: 1.0.0.beta.1
(function() {
Ember.EasyForm = Ember.Namespace.create({
VERSION: '1.0.0.beta.1'
});
})();
(function() {
Ember.EasyForm.Config = Ember.Namespace.create({
_wrappers: {
'default': {
formClass: '',
fieldErrorClass: 'fieldWithErrors',
inputClass: 'input',
errorClass: 'error',
hintClass: 'hint',
labelClass: '',
wrapControls: false,
controlsWrapperClass: ''
}
},
_inputTypes: {},
registerWrapper: function(name, wrapper) {
this._wrappers[name] = Ember.$.extend({}, this._wrappers['default'], wrapper);
},
getWrapper: function(name) {
var wrapper = this._wrappers[name];
Ember.assert("The wrapper '" + name + "' was not registered.", wrapper);
return wrapper;
},
registerInputType: function(name, type){
this._inputTypes[name] = type;
},
getInputType: function(name) {
return this._inputTypes[name];
}
});
})();
(function() {
Ember.Handlebars.registerHelper('error-field', function(property, options) {
options = Ember.EasyForm.processOptions(property, options);
if (options.hash.propertyBinding) {
options.hash.property = Ember.Handlebars.get(this, options.hash.propertyBinding, options);
}
return Ember.Handlebars.helpers.view.call(this, Ember.EasyForm.Error, options);
});
})();
(function() {
Ember.Handlebars.registerHelper('form-for', function(object, options) {
options.hash.contentBinding = object;
return Ember.Handlebars.helpers.view.call(this, Ember.EasyForm.Form, options);
});
})();
(function() {
Ember.Handlebars.registerHelper('hint-field', function(property, options) {
options = Ember.EasyForm.processOptions(property, options);
if (options.hash.text || options.hash.textBinding) {
return Ember.Handlebars.helpers.view.call(this, Ember.EasyForm.Hint, options);
}
});
})();
(function() {
Ember.Handlebars.registerHelper('input', function(property, options) {
options = Ember.EasyForm.processOptions(property, options);
options.hash.isBlock = !!(options.fn);
return Ember.Handlebars.helpers.view.call(this, Ember.EasyForm.Input, options);
});
})();
(function() {
Ember.Handlebars.registerHelper('input-field', function(property, options) {
options = Ember.EasyForm.processOptions(property, options);
if (options.hash.propertyBinding) {
options.hash.property = Ember.Handlebars.get(this, options.hash.propertyBinding, options);
}
if (options.hash.inputOptionsBinding) {
options.hash.inputOptions = Ember.Handlebars.get(this, options.hash.inputOptionsBinding, options);
}
property = options.hash.property;
var context = this,
propertyType = function(property) {
var constructor = (context.get('content') || context).constructor;
if (constructor.proto) {
return Ember.meta(constructor.proto(), false).descs[property];
} else {
return null;
}
};
options.hash.valueBinding = property;
options.hash.viewName = 'input-field-'+options.data.view.elementId;
if (options.hash.inputOptions) {
var inputOptions = options.hash.inputOptions, optionName;
for (optionName in inputOptions) {
if (inputOptions.hasOwnProperty(optionName)) {
options.hash[optionName] = inputOptions[optionName];
}
}
delete options.hash.inputOptions;
}
if (options.hash.as === 'text') {
return Ember.Handlebars.helpers.view.call(context, Ember.EasyForm.TextArea, options);
} else if (options.hash.as === 'select') {
delete(options.hash.valueBinding);
options.hash.contentBinding = options.hash.collection;
options.hash.selectionBinding = options.hash.selection;
options.hash.valueBinding = options.hash.value;
if (Ember.isNone(options.hash.selectionBinding) && Ember.isNone(options.hash.valueBinding)) {
options.hash.selectionBinding = property;
}
return Ember.Handlebars.helpers.view.call(context, Ember.EasyForm.Select, options);
} else if (options.hash.as === 'checkbox') {
if (Ember.isNone(options.hash.checkedBinding)) {
options.hash.checkedBinding = property;
}
return Ember.Handlebars.helpers.view.call(context, Ember.EasyForm.Checkbox, options);
} else {
if (!options.hash.as) {
if (property.match(/password/)) {
options.hash.type = 'password';
} else if (property.match(/email/)) {
options.hash.type = 'email';
} else if (property.match(/url/)) {
options.hash.type = 'url';
} else if (property.match(/color/)) {
options.hash.type = 'color';
} else if (property.match(/^tel/)) {
options.hash.type = 'tel';
} else if (property.match(/search/)) {
options.hash.type = 'search';
} else {
if (propertyType(property) === 'number' || typeof(context.get(property)) === 'number') {
options.hash.type = 'number';
} else if (propertyType(property) === 'date' || (!Ember.isNone(context.get(property)) && context.get(property).constructor === Date)) {
options.hash.type = 'date';
} else if (propertyType(property) === 'boolean' || (!Ember.isNone(context.get(property)) && context.get(property).constructor === Boolean)) {
options.hash.checkedBinding = property;
return Ember.Handlebars.helpers.view.call(context, Ember.EasyForm.Checkbox, options);
}
}
} else {
var inputType = Ember.EasyForm.Config.getInputType(options.hash.as);
if (inputType) {
return Ember.Handlebars.helpers.view.call(context, inputType, options);
}
options.hash.type = options.hash.as;
}
return Ember.Handlebars.helpers.view.call(context, Ember.EasyForm.TextField, options);
}
});
})();
(function() {
Ember.Handlebars.registerHelper('label-field', function(property, options) {
options = Ember.EasyForm.processOptions(property, options);
options.hash.viewName = 'label-field-'+options.data.view.elementId;
return Ember.Handlebars.helpers.view.call(this, Ember.EasyForm.Label, options);
});
})();
(function() {
Ember.Handlebars.registerHelper('submit', function(value, options) {
if (typeof(value) === 'object') {
options = value;
value = undefined;
}
options.hash.context = this;
options.hash.value = value || 'Submit';
return (options.hash.as === 'button') ?
Ember.Handlebars.helpers.view.call(this, Ember.EasyForm.Button, options)
:
Ember.Handlebars.helpers.view.call(this, Ember.EasyForm.Submit, options);
});
})();
(function() {
})();
(function() {
Ember.EasyForm.BaseView = Ember.View.extend({
getWrapperConfig: function(configName) {
var wrapper = Ember.EasyForm.Config.getWrapper(this.get('wrapper'));
return wrapper[configName];
},
wrapper: Ember.computed(function() {
// Find the first parent with 'wrapper' defined.
var parentView = this.get('parentView');
while(parentView){
var config = parentView.get('wrapper');
if (config) return config;
parentView = parentView.get('parentView');
}
return 'default';
})
});
})();
(function() {
Ember.EasyForm.Checkbox = Ember.Checkbox.extend();
})();
(function() {
Ember.EasyForm.Error = Ember.EasyForm.BaseView.extend({
tagName: 'span',
init: function() {
this._super();
this.classNames.push(this.getWrapperConfig('errorClass'));
Ember.Binding.from('context.errors.' + this.property).to('errors').connect(this);
},
templateName: 'easyForm/error'
});
})();
(function() {
Ember.EasyForm.Form = Ember.EasyForm.BaseView.extend({
tagName: 'form',
attributeBindings: ['novalidate'],
novalidate: 'novalidate',
wrapper: 'default',
init: function() {
this._super();
this.classNames.push(this.getWrapperConfig('formClass'));
this.action = this.action || 'submit';
},
submit: function(event) {
var _this = this, promise;
if (event) {
event.preventDefault();
}
if (Ember.isNone(this.get('context.validate'))) {
this.get('controller').send(this.action);
} else {
if (!Ember.isNone(this.get('context').validate)) {
promise = this.get('context').validate();
} else {
promise = this.get('context.content').validate();
}
promise.then(function() {
if (_this.get('context.isValid')) {
_this.get('controller').send(_this.action);
}
});
}
}
});
})();
(function() {
Ember.EasyForm.Hint = Ember.EasyForm.BaseView.extend({
tagName: 'span',
init: function() {
this._super();
this.classNames.push(this.getWrapperConfig('hintClass'));
},
render: function(buffer) {
buffer.push(Handlebars.Utils.escapeExpression(this.get('text')));
},
textChanged: function() {
this.rerender();
}.observes('text')
});
})();
(function() {
Ember.EasyForm.Input = Ember.EasyForm.BaseView.extend({
init: function() {
this._super();
this.classNameBindings.push('showError:' + this.getWrapperConfig('fieldErrorClass'));
this.classNames.push(this.getWrapperConfig('inputClass'));
Ember.defineProperty(this, 'showError', Ember.computed.and('canShowValidationError', 'context.errors.' + this.property + '.firstObject'));
if (!this.isBlock) {
if (this.getWrapperConfig('wrapControls')) {
this.set('templateName', 'easyForm/wrapped_input');
} else {
this.set('templateName', 'easyForm/input');
}
}
},
setupValidationDependencies: function() {
var keys = this.get('context._dependentValidationKeys'), key;
if (keys) {
for(key in keys) {
if (keys[key].contains(this.property)) {
this._keysForValidationDependencies.pushObject(key);
}
}
}
}.on('init'),
_keysForValidationDependencies: Ember.A(),
dependentValidationKeyCanTrigger: false,
tagName: 'div',
classNames: ['string'],
didInsertElement: function() {
this.set('label-field-'+this.elementId+'.for', this.get('input-field-'+this.elementId+'.elementId'));
},
concatenatedProperties: ['inputOptions', 'bindableInputOptions'],
inputOptions: ['as', 'collection', 'optionValuePath', 'optionLabelPath', 'selection', 'value'],
bindableInputOptions: ['placeholder', 'prompt'],
controlsWrapperClass: function() {
return this.getWrapperConfig('controlsWrapperClass');
}.property(),
inputOptionsValues: function() {
var options = {}, i, key, keyBinding, inputOptions = this.inputOptions, bindableInputOptions = this.bindableInputOptions;
for (i = 0; i < inputOptions.length; i++) {
key = inputOptions[i];
if (this[key]) {
if (typeof(this[key]) === 'boolean') {
this[key] = key;
}
options[key] = this[key];
}
}
for (i = 0; i < bindableInputOptions.length; i++) {
key = bindableInputOptions[i];
keyBinding = key + 'Binding';
if (this[key] || this[keyBinding]) {
options[keyBinding] = 'view.' + key;
}
}
return options;
}.property(),
focusOut: function() {
this.set('hasFocusedOut', true);
this.showValidationError();
},
showValidationError: function() {
if (this.get('hasFocusedOut')) {
if (Ember.isEmpty(this.get('context.errors.' + this.property))) {
this.set('canShowValidationError', false);
} else {
this.set('canShowValidationError', true);
}
}
},
input: function() {
this._keysForValidationDependencies.forEach(function(key) {
this.get('parentView.childViews').forEach(function(view) {
if (view.property === key) {
view.showValidationError();
}
}, this);
}, this);
}
});
})();
(function() {
Ember.EasyForm.Label = Ember.EasyForm.BaseView.extend({
tagName: 'label',
attributeBindings: ['for'],
labelText: function() {
return this.get('text') || this.get('property').underscore().split('_').join(' ').capitalize();
}.property('text', 'property'),
init: function() {
this._super();
this.classNames.push(this.getWrapperConfig('labelClass'));
},
render: function(buffer) {
buffer.push(Handlebars.Utils.escapeExpression(this.get('labelText')));
},
labelTextChanged: function() {
this.rerender();
}.observes('labelText')
});
})();
(function() {
Ember.EasyForm.Select = Ember.Select.extend();
})();
(function() {
Ember.EasyForm.Submit = Ember.View.extend({
tagName: 'input',
attributeBindings: ['type', 'value', 'disabled'],
type: 'submit',
disabled: function() {
return this.get('context.isInvalid');
}.property('context.isInvalid'),
init: function() {
this._super();
this.set('value', this.value);
}
});
})();
(function() {
Ember.EasyForm.Button = Ember.View.extend({
tagName: 'button',
template: Ember.Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Ember.Handlebars.helpers); data = data || {};
var hashTypes, hashContexts, escapeExpression=this.escapeExpression;
hashTypes = {};
hashContexts = {};
data.buffer.push(escapeExpression(helpers._triageMustache.call(depth0, "text", {hash:{},contexts:[depth0],types:["ID"],hashContexts:hashContexts,hashTypes:hashTypes,data:data})));
}),
attributeBindings: ['type', 'disabled'],
type: 'submit',
disabled: function() {
return this.get('context.isInvalid');
}.property('context.isInvalid'),
init: function() {
this._super();
this.set('context.text', this.value);
}
});
})();
(function() {
Ember.EasyForm.TextArea = Ember.TextArea.extend();
})();
(function() {
Ember.EasyForm.TextField = Ember.TextField.extend();
})();
(function() {
})();
(function() {
Ember.TEMPLATES['easyForm/error'] = Ember.Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Ember.Handlebars.helpers); data = data || {};
var hashTypes, hashContexts, escapeExpression=this.escapeExpression;
hashTypes = {};
hashContexts = {};
data.buffer.push(escapeExpression(helpers._triageMustache.call(depth0, "view.errors.firstObject", {hash:{},contexts:[depth0],types:["ID"],hashContexts:hashContexts,hashTypes:hashTypes,data:data})));
});
})();
(function() {
Ember.TEMPLATES['easyForm/input'] = Ember.Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Ember.Handlebars.helpers); data = data || {};
var buffer = '', stack1, hashContexts, hashTypes, options, helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression;
hashContexts = {'propertyBinding': depth0,'textBinding': depth0};
hashTypes = {'propertyBinding': "STRING",'textBinding': "STRING"};
options = {hash:{
'propertyBinding': ("view.property"),
'textBinding': ("view.label")
},contexts:[],types:[],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers['label-field'] || depth0['label-field']),stack1 ? stack1.call(depth0, options) : helperMissing.call(depth0, "label-field", options))));
hashTypes = {};
hashContexts = {};
options = {hash:{},contexts:[depth0],types:["STRING"],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers.partial || depth0.partial),stack1 ? stack1.call(depth0, "easyForm/inputControls", options) : helperMissing.call(depth0, "partial", "easyForm/inputControls", options))));
return buffer;
});
})();
(function() {
Ember.TEMPLATES['easyForm/inputControls'] = Ember.Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Ember.Handlebars.helpers); data = data || {};
var buffer = '', stack1, stack2, hashContexts, hashTypes, options, helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression, self=this;
function program1(depth0,data) {
var stack1, hashContexts, hashTypes, options;
hashContexts = {'propertyBinding': depth0};
hashTypes = {'propertyBinding': "STRING"};
options = {hash:{
'propertyBinding': ("view.property")
},contexts:[],types:[],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers['error-field'] || depth0['error-field']),stack1 ? stack1.call(depth0, options) : helperMissing.call(depth0, "error-field", options))));
}
function program3(depth0,data) {
var stack1, hashContexts, hashTypes, options;
hashContexts = {'propertyBinding': depth0,'textBinding': depth0};
hashTypes = {'propertyBinding': "STRING",'textBinding': "STRING"};
options = {hash:{
'propertyBinding': ("view.property"),
'textBinding': ("view.hint")
},contexts:[],types:[],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers['hint-field'] || depth0['hint-field']),stack1 ? stack1.call(depth0, options) : helperMissing.call(depth0, "hint-field", options))));
}
hashContexts = {'propertyBinding': depth0,'inputOptionsBinding': depth0};
hashTypes = {'propertyBinding': "STRING",'inputOptionsBinding': "STRING"};
options = {hash:{
'propertyBinding': ("view.property"),
'inputOptionsBinding': ("view.inputOptionsValues")
},contexts:[],types:[],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers['input-field'] || depth0['input-field']),stack1 ? stack1.call(depth0, options) : helperMissing.call(depth0, "input-field", options))));
hashTypes = {};
hashContexts = {};
stack2 = helpers['if'].call(depth0, "view.showError", {hash:{},inverse:self.noop,fn:self.program(1, program1, data),contexts:[depth0],types:["ID"],hashContexts:hashContexts,hashTypes:hashTypes,data:data});
if(stack2 || stack2 === 0) { data.buffer.push(stack2); }
hashTypes = {};
hashContexts = {};
stack2 = helpers['if'].call(depth0, "view.hint", {hash:{},inverse:self.noop,fn:self.program(3, program3, data),contexts:[depth0],types:["ID"],hashContexts:hashContexts,hashTypes:hashTypes,data:data});
if(stack2 || stack2 === 0) { data.buffer.push(stack2); }
return buffer;
});
})();
(function() {
Ember.TEMPLATES['easyForm/wrapped_input'] = Ember.Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Ember.Handlebars.helpers); data = data || {};
var buffer = '', stack1, hashContexts, hashTypes, options, helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression;
hashContexts = {'propertyBinding': depth0,'textBinding': depth0};
hashTypes = {'propertyBinding': "STRING",'textBinding': "STRING"};
options = {hash:{
'propertyBinding': ("view.property"),
'textBinding': ("view.label")
},contexts:[],types:[],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers['label-field'] || depth0['label-field']),stack1 ? stack1.call(depth0, options) : helperMissing.call(depth0, "label-field", options))));
data.buffer.push("<div class=\"");
hashTypes = {};
hashContexts = {};
data.buffer.push(escapeExpression(helpers.unbound.call(depth0, "view.controlsWrapperClass", {hash:{},contexts:[depth0],types:["ID"],hashContexts:hashContexts,hashTypes:hashTypes,data:data})));
data.buffer.push("\">");
hashTypes = {};
hashContexts = {};
options = {hash:{},contexts:[depth0],types:["STRING"],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers.partial || depth0.partial),stack1 ? stack1.call(depth0, "easyForm/inputControls", options) : helperMissing.call(depth0, "partial", "easyForm/inputControls", options))));
data.buffer.push("</div>");
return buffer;
});
})();
(function() {
Ember.EasyForm.TEMPLATES = {};
})();
(function() {
Ember.EasyForm.objectNameFor = function(object) {
var constructorArray = object.constructor.toString().split('.');
return constructorArray[constructorArray.length - 1].underscore();
};
Ember.EasyForm.processOptions = function(property, options) {
if (options) {
options.hash.property = property;
} else {
options = property;
}
return options;
};
})();
(function() {
})();
// ==========================================================================
// Project: Ember Validations
// Copyright: Copyright 2013 DockYard, LLC. and contributors.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
// Version: 1.0.0.beta.1
(function() {
Ember.Validations = Ember.Namespace.create({
VERSION: '1.0.0.beta.1'
});
})();
(function() {
Ember.Validations.messages = {
render: function(attribute, context) {
var regex = new RegExp("{{(.*?)}}"),
attributeName = "";
if (regex.test(this.defaults[attribute])) {
attributeName = regex.exec(this.defaults[attribute])[1];
}
return this.defaults[attribute].replace(regex, context[attributeName]);
},
defaults: {
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"
}
};
})();
(function() {
Ember.Validations.Errors = Ember.Object.extend({
unknownProperty: function(property) {
this.set(property, Ember.makeArray());
return this.get(property);
}
});
})();
(function() {
var setValidityMixin = Ember.Mixin.create({
setValidity: function() {
if (this.get('validators').compact().filterProperty('isValid', false).get('length') > 0) {
if (this.get('isValid') === false) {
this.notifyPropertyChange('isValid');
} else {
this.set('isValid', false);
}
} else {
if (this.get('isValid') === true) {
this.notifyPropertyChange('isValid');
} else {
this.set('isValid', true);
}
}
}.on('init')
});
var pushValidatableObject = function(model, property) {
model.removeObserver(property, pushValidatableObject);
if (model.get(property).constructor === Array) {
model.validators.pushObject(ArrayValidatorProxy.create({model: model, property: property, content: model.get(property)}));
} else {
model.validators.pushObject(model.get(property));
}
};
var findValidator = function(validator) {
var klass = validator.classify();
return Ember.Validations.validators.local[klass] || Ember.Validations.validators.remote[klass];
};
var ArrayValidatorProxy = Ember.ArrayProxy.extend(setValidityMixin, {
init: function() {
this._super();
this.addObserver('@each.isValid', this, this.setValidity);
this.model.addObserver(''+this.property+'.[]', this, this.setValidity);
},
validate: function() {
var promises;
promises = this.get('content').map(function(validator) {
return validator.validate();
}).without(undefined);
return Ember.RSVP.all(promises);
}.on('init'),
validators: Ember.computed.alias('content')
});
Ember.Validations.Mixin = Ember.Mixin.create(setValidityMixin, {
init: function() {
this._super();
this.errors = Ember.Validations.Errors.create();
this._dependentValidationKeys = {};
this.validators = Ember.makeArray();
this.isValid = undefined;
if (this.get('validations') === undefined) {
this.validations = {};
}
this.buildValidators();
this.addObserver('[email protected]', this, this.setValidity);
this.validators.forEach(function(validator) {
validator.addObserver('errors.[]', this, function(sender, key, value, context, rev) {
var errors = Ember.makeArray();
this.validators.forEach(function(validator) {
if (validator.property === sender.property) {
errors = errors.concat(validator.errors);
}
}, this);
this.set('errors.' + sender.property, errors);
});
}, this);
},
isInvalid: function() {
return !this.get('isValid');
}.property('isValid'),
buildValidators: function() {
var property, validator;
for (property in this.validations) {
if (this.validations[property].constructor === Object) {
this.buildRuleValidator(property);
} else {
this.buildObjectValidator(property);
}
}
},
buildRuleValidator: function(property) {
var validator;
for (validator in this.validations[property]) {
if (this.validations[property].hasOwnProperty(validator)) {
this.validators.pushObject(findValidator(validator).create({model: this, property: property, options: this.validations[property][validator]}));
}
}
},
buildObjectValidator: function(property) {
if (Ember.isNone(this.get(property))) {
this.addObserver(property, this, pushValidatableObject);
} else {
pushValidatableObject(this, property);
}
},
validate: function() {
var promises = this.validators.map(function(validator) {
return validator.validate();
}).without(undefined);
return Ember.RSVP.all(promises);
}.on('init')
});
})();
(function() {
Ember.Validations.patterns = Ember.Namespace.create({
numericality: /^(-|\+)?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d*)?$/,
blank: /^\s*$/
});
})();
(function() {
Ember.Validations.validators = Ember.Namespace.create();
Ember.Validations.validators.local = Ember.Namespace.create();
Ember.Validations.validators.remote = Ember.Namespace.create();
})();
(function() {
Ember.Validations.validators.Base = Ember.Object.extend({
init: function() {
this.set('errors', Ember.makeArray());
this.isValid = undefined;
this._dependentValidationKeys = Ember.makeArray();
this.conditionals = {
'if': this.get('options.if'),
unless: this.get('options.unless')
};
this.model.addObserver(this.property, this, this.validate);
},
addObserversForDependentValidationKeys: function() {
this._dependentValidationKeys.forEach(function(key) {
this.model.addObserver(key, this, this.validate);
}, this);
}.on('init'),
pushDependentValidaionKeyToModel: function() {
var model = this.get('model');
if (model._dependentValidationKeys[this.property] === undefined) {
model._dependentValidationKeys[this.property] = Ember.makeArray();
}
model._dependentValidationKeys[this.property].addObjects(this._dependentValidationKeys);
}.on('init'),
call: function () {
throw 'Not implemented!';
},
unknownProperty: function(key) {
var model = this.get('model');
if (model) {
return model.get(key);
}
},
validate: function() {
if (this.canValidate()) {
this.errors.clear();
this.call();
if (this.errors.length > 0) {
if (this.get('isValid') === false) {
this.notifyPropertyChange('isValid');
} else {
this.set('isValid', false);
}
return Ember.RSVP.reject();
} else {
if (this.get('isValid') === true) {
this.notifyPropertyChange('isValid');
} else {
this.set('isValid', true);
}
return Ember.RSVP.resolve();
}
}
}.on('init'),
canValidate: function() {
if (typeof(this.conditionals) === 'object') {
if (this.conditionals['if']) {
if (typeof(this.conditionals['if']) === 'function') {
return this.conditionals['if'](this.model);
} else if (typeof(this.conditionals['if']) === 'string') {
if (typeof(this.model[this.conditionals['if']]) === 'function') {
return this.model[this.conditionals['if']]();
} else {
return this.model.get(this.conditionals['if']);
}
}
} else if (this.conditionals.unless) {
if (typeof(this.conditionals.unless) === 'function') {
return !this.conditionals.unless(this.model);
} else if (typeof(this.conditionals.unless) === 'string') {
if (typeof(this.model[this.conditionals.unless]) === 'function') {
return !this.model[this.conditionals.unless]();
} else {
return !this.model.get(this.conditionals.unless);
}
}
} else {
return true;
}
} else {
return true;
}
}
});
})();
(function() {
Ember.Validations.validators.local.Absence = Ember.Validations.validators.Base.extend({
init: function() {
this._super();
/*jshint expr:true*/
if (this.options === true) {
this.set('options', {});
}
if (this.options.message === undefined) {
this.set('options.message', Ember.Validations.messages.render('present', this.options));
}
},
call: function() {
if (!Ember.isEmpty(this.model.get(this.property))) {
this.errors.pushObject(this.options.message);
}
}
});
})();
(function() {
Ember.Validations.validators.local.Acceptance = Ember.Validations.validators.Base.extend({
init: function() {
this._super();
/*jshint expr:true*/
if (this.options === true) {
this.set('options', {});
}
if (this.options.message === undefined) {
this.set('options.message', Ember.Validations.messages.render('accepted', this.options));
}
},
call: function() {
if (this.options.accept) {
if (this.model.get(this.property) !== this.options.accept) {
this.errors.pushObject(this.options.message);
}
} else if (this.model.get(this.property) !== '1' && this.model.get(this.property) !== 1 && this.model.get(this.property) !== true) {
this.errors.pushObject(this.options.message);
}
}
});
})();
(function() {
Ember.Validations.validators.local.Confirmation = Ember.Validations.validators.Base.extend({
init: function() {
this.originalProperty = this.property;
this.property = this.property + 'Confirmation';
this._super();
this._dependentValidationKeys.pushObject(this.originalProperty);
/*jshint expr:true*/
if (this.options === true) {
this.set('options', { attribute: this.originalProperty });
this.set('options', { message: Ember.Validations.messages.render('confirmation', this.options) });
}
},
call: function() {
if (this.model.get(this.originalProperty) !== this.model.get(this.property)) {
this.errors.pushObject(this.options.message);
}
}
});
})();
(function() {
Ember.Validations.validators.local.Exclusion = Ember.Validations.validators.Base.extend({
init: function() {
this._super();
if (this.options.constructor === Array) {
this.set('options', { 'in': this.options });
}
if (this.options.message === undefined) {
this.set('options.message', Ember.Validations.messages.render('exclusion', this.options));
}
},
call: function() {
/*jshint expr:true*/
var message, lower, upper;
if (Ember.isEmpty(this.model.get(this.property))) {
if (this.options.allowBlank === undefined) {
this.errors.pushObject(this.options.message);
}
} else if (this.options['in']) {
if (Ember.$.inArray(this.model.get(this.property), this.options['in']) !== -1) {
this.errors.pushObject(this.options.message);
}
} else if (this.options.range) {
lower = this.options.range[0];
upper = this.options.range[1];
if (this.model.get(this.property) >= lower && this.model.get(this.property) <= upper) {
this.errors.pushObject(this.options.message);
}
}
}
});
})();
(function() {
Ember.Validations.validators.local.Format = Ember.Validations.validators.Base.extend({
init: function() {
this._super();
if (this.options.constructor === RegExp) {
this.set('options', { 'with': this.options });
}
if (this.options.message === undefined) {
this.set('options.message', Ember.Validations.messages.render('invalid', this.options));
}
},
call: function() {
if (Ember.isEmpty(this.model.get(this.property))) {
if (this.options.allowBlank === undefined) {
this.errors.pushObject(this.options.message);
}
} else if (this.options['with'] && !this.options['with'].test(this.model.get(this.property))) {
this.errors.pushObject(this.options.message);
} else if (this.options.without && this.options.without.test(this.model.get(this.property))) {
this.errors.pushObject(this.options.message);
}
}
});
})();
(function() {
Ember.Validations.validators.local.Inclusion = Ember.Validations.validators.Base.extend({
init: function() {
this._super();
if (this.options.constructor === Array) {
this.set('options', { 'in': this.options });
}
if (this.options.message === undefined) {
this.set('options.message', Ember.Validations.messages.render('inclusion', this.options));
}
},
call: function() {
var message, lower, upper;
if (Ember.isEmpty(this.model.get(this.property))) {
if (this.options.allowBlank === undefined) {
this.errors.pushObject(this.options.message);
}
} else if (this.options['in']) {
if (Ember.$.inArray(this.model.get(this.property), this.options['in']) === -1) {
this.errors.pushObject(this.options.message);
}
} else if (this.options.range) {
lower = this.options.range[0];
upper = this.options.range[1];
if (this.model.get(this.property) < lower || this.model.get(this.property) > upper) {
this.errors.pushObject(this.options.message);
}
}
}
});
})();
(function() {
Ember.Validations.validators.local.Length = Ember.Validations.validators.Base.extend({
init: function() {
var index, key;
this._super();
/*jshint expr:true*/
if (typeof(this.options) === 'number') {
this.set('options', { 'is': this.options });
}
if (this.options.messages === undefined) {
this.set('options.messages', {});
}
for (index = 0; index < this.messageKeys().length; index++) {
key = this.messageKeys()[index];
if (this.options[key] !== undefined && this.options[key].constructor === String) {
this.model.addObserver(this.options[key], this, this.validate);
}
}
this.tokenizedLength = new Function('value', 'return (value || "").' + (this.options.tokenizer || 'split("")') + '.length');
},
CHECKS: {
'is' : '==',
'minimum' : '>=',
'maximum' : '<='
},
MESSAGES: {
'is' : 'wrongLength',
'minimum' : 'tooShort',
'maximum' : 'tooLong'
},
getValue: function(key) {
if (this.options[key].constructor === String) {
return this.model.get(this.options[key]) || 0;
} else {
return this.options[key];
}
},
messageKeys: function() {
return Object.keys(this.MESSAGES);
},
checkKeys: function() {
return Object.keys(this.CHECKS);
},
renderMessageFor: function(key) {
var options = {count: this.getValue(key)}, _key;
for (_key in this.options) {
options[_key] = this.options[_key];
}
return this.options.messages[this.MESSAGES[key]] || Ember.Validations.messages.render(this.MESSAGES[key], options);
},
renderBlankMessage: function() {
if (this.options.is) {
return this.renderMessageFor('is');
} else if (this.options.minimum) {
return this.renderMessageFor('minimum');
}
},
call: function() {
var check, fn, message, operator, key;
if (Ember.isEmpty(this.model.get(this.property))) {
if (this.options.allowBlank === undefined && (this.options.is || this.options.minimum)) {
this.errors.pushObject(this.renderBlankMessage());
}
} else {
for (key in this.CHECKS) {
operator = this.CHECKS[key];
if (!this.options[key]) {
continue;
}
fn = new Function('return ' + this.tokenizedLength(this.model.get(this.property)) + ' ' + operator + ' ' + this.getValue(key));
if (!fn()) {
this.errors.pushObject(this.renderMessageFor(key));
}
}
}
}
});
})();
(function() {
Ember.Validations.validators.local.Numericality = Ember.Validations.validators.Base.extend({
init: function() {
/*jshint expr:true*/
var index, keys, key;
this._super();
if (this.options === true) {
this.options = {};
} else if (this.options.constructor === String) {
key = this.options;
this.options = {};
this.options[key] = true;
}
if (this.options.messages === undefined) {
this.options.messages = { numericality: Ember.Validations.messages.render('notANumber', this.options) };
}
if (this.options.onlyInteger !== undefined && this.options.messages.onlyInteger === undefined) {
this.options.messages.onlyInteger = Ember.Validations.messages.render('notAnInteger', this.options);
}
keys = Object.keys(this.CHECKS).concat(['odd', 'even']);
for(index = 0; index < keys.length; index++) {
key = keys[index];
if (isNaN(this.options[key])) {
this.model.addObserver(this.options[key], this, this.validate);
}
if (this.options[key] !== undefined && this.options.messages[key] === undefined) {
if (Ember.$.inArray(key, Object.keys(this.CHECKS)) !== -1) {
this.options.count = this.options[key];
}
this.options.messages[key] = Ember.Validations.messages.render(key, this.options);
if (this.options.count !== undefined) {
delete this.options.count;
}
}
}
},
CHECKS: {
equalTo :'===',
greaterThan : '>',
greaterThanOrEqualTo : '>=',
lessThan : '<',
lessThanOrEqualTo : '<='
},
call: function() {
var check, checkValue, fn, form, operator, val;
if (Ember.isEmpty(this.model.get(this.property))) {
if (this.options.allowBlank === undefined) {
this.errors.pushObject(this.options.messages.numericality);
}
} else if (!Ember.Validations.patterns.numericality.test(this.model.get(this.property))) {
this.errors.pushObject(this.options.messages.numericality);
} else if (this.options.onlyInteger === true && !(/^[+\-]?\d+$/.test(this.model.get(this.property)))) {
this.errors.pushObject(this.options.messages.onlyInteger);
} else if (this.options.odd && parseInt(this.model.get(this.property), 10) % 2 === 0) {
this.errors.pushObject(this.options.messages.odd);
} else if (this.options.even && parseInt(this.model.get(this.property), 10) % 2 !== 0) {
this.errors.pushObject(this.options.messages.even);
} else {
for (check in this.CHECKS) {
operator = this.CHECKS[check];
if (this.options[check] === undefined) {
continue;
}
if (!isNaN(parseFloat(this.options[check])) && isFinite(this.options[check])) {
checkValue = this.options[check];
} else if (this.model.get(this.options[check]) !== undefined) {
checkValue = this.model.get(this.options[check]);
}
fn = new Function('return ' + this.model.get(this.property) + ' ' + operator + ' ' + checkValue);
if (!fn()) {
this.errors.pushObject(this.options.messages[check]);
}
}
}
}
});
})();
(function() {
Ember.Validations.validators.local.Presence = Ember.Validations.validators.Base.extend({
init: function() {
this._super();
/*jshint expr:true*/
if (this.options === true) {
this.options = {};
}
if (this.options.message === undefined) {
this.options.message = Ember.Validations.messages.render('blank', this.options);
}
},
call: function() {
if (Ember.isEmpty(this.model.get(this.property))) {
this.errors.pushObject(this.options.message);
}
}
});
})();
(function() {
})();
(function() {
Ember.ControllerMixin.reopen({
childControllers: Ember.A(),
setupAsChildController: function() {
if (this.parentController) {
this.parentController.childControllers.pushObject(this);
this.reopen({
willDestroy: function() {
this._super();
this.parentController.childControllers.removeObject(this);
}
});
}
}.on('init')
});
})();
(function() {
})();
(function() {
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment