ember-cli-simple-validation
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -6,9 +6,12 @@ import Ember from "ember"; | ||
classNameBindings: ["visible"], | ||
attributeBindings: ["className", "model", "field", "validation", "submitted", "delayed"], | ||
attributeBindings: ["className", "model", "field", "validation", "submitted", "delayed", "index"], | ||
visible: function() { | ||
var className = this.get("className"); | ||
var field = this.get("field"); | ||
var index = this.get("index"); | ||
var isDirty = this.get("isDirty"); | ||
var validation = this.get("validation"); | ||
var delayed = this.get("delayed"); | ||
var submitted = this.get("submitted"); | ||
var delayed = this.get("delayed"); | ||
var className = this.get("className"); | ||
var validationIndex = this.get("targetObject.%@1%@2Validation".fmt(field, index)); | ||
var validation = validationIndex || this.get("validation"); | ||
var condition = delayed === true ? !validation && submitted : !validation && (isDirty || submitted); | ||
@@ -19,5 +22,11 @@ if(condition) { | ||
return className ? "hidden %@".fmt(className) : "hidden"; | ||
}.property("validation", "isDirty", "submitted"), | ||
}.property("validation", "isDirty", "submitted", "fieldName"), | ||
initialize: function() { | ||
var isDirty = "model.%@IsDirty".fmt(this.get("field")); | ||
var field = this.get("field"); | ||
var index = this.get("index"); | ||
if(index >= 0) { | ||
var fieldName = "model.%@".fmt(field); | ||
Ember.Binding.from(fieldName).to("fieldName").connect(this); | ||
} | ||
var isDirty = "model.%@IsDirty".fmt(field); | ||
Ember.Binding.from(isDirty).to("isDirty").connect(this); | ||
@@ -24,0 +33,0 @@ }.on("init") |
import Ember from "ember"; | ||
var dynamicEachAttrs = []; | ||
function factory(mixin) { | ||
return mixin.get("constructor.ClassMixin.ownerConstructor"); | ||
} | ||
function eachAttrs(mixin) { | ||
var attributes = []; | ||
dynamicEachAttrs = []; | ||
factory(mixin).eachComputedProperty(function(field, meta) { | ||
if (meta.validateEach) { | ||
dynamicEachAttrs.push(field); | ||
attributes.push({field: field, options: meta.options}); | ||
} | ||
}); | ||
return attributes; | ||
} | ||
function attrs(mixin) { | ||
var attributes = []; | ||
var factory = mixin.get("constructor.ClassMixin.ownerConstructor"); | ||
factory.eachComputedProperty(function (key) { | ||
if (key.indexOf("Validation") > 0) { | ||
attributes.push(key); | ||
factory(mixin).eachComputedProperty(function(field) { | ||
if (field.indexOf("Validation") > 0) { | ||
attributes.push(field); | ||
} | ||
}); | ||
dynamicEachAttrs.forEach(function(field) { | ||
mixin.get("model").forEach(function(model, index) { | ||
attributes.push("%@1%@2Validation".fmt(field, index)); | ||
}); | ||
}); | ||
return attributes; | ||
} | ||
function run(value, options, context) { | ||
context.notifyPropertyChange("valid"); | ||
if (options instanceof RegExp) { | ||
return options.test(value); | ||
} | ||
if (options instanceof Function) { | ||
return options.apply(context, arguments); | ||
} | ||
if (typeof value === "boolean") { | ||
return value; | ||
} | ||
if (typeof value === "string") { | ||
return value.trim().length > 0; | ||
} | ||
return value; | ||
} | ||
var ValidationMixin = Ember.Mixin.create({ | ||
validate: function() { | ||
valid: function() { | ||
var self = this; | ||
var attributes = attrs(this); | ||
Ember.defineProperty(this, "valid", Ember.computed(function() { | ||
var result = true; | ||
attributes.forEach(function(attr) { | ||
result = self.get(attr) && result; | ||
var result = true; | ||
attrs(this).forEach(function(attr) { | ||
result = self.get(attr) && result; | ||
}); | ||
return result; | ||
}.property(), | ||
each: function() { | ||
var self = this; | ||
eachAttrs(this).forEach(function(attr) { | ||
self.get("model").forEach(function(obj, index) { | ||
Ember.defineProperty(self, "%@1%@2Validation".fmt(attr.field, index), Ember.computed(function() { | ||
if(self.get("model").objectAt(index)) { | ||
var value = self.get("model").objectAt(index).getWithDefault(attr.field, ""); | ||
return run(value, attr.options, self, index); | ||
} | ||
}).property("model.@each." + attr.field)); | ||
}); | ||
return result; | ||
}).property("" + attributes)); | ||
}.on("init") | ||
}); | ||
}.observes("model.@each.isDone") | ||
}); | ||
@@ -31,18 +80,12 @@ | ||
var value = this.getWithDefault(field, ""); | ||
if (options instanceof RegExp) { | ||
return options.test(value); | ||
} | ||
if (options instanceof Function) { | ||
return options.apply(this, arguments); | ||
} | ||
if (typeof value === "boolean") { | ||
return value; | ||
} | ||
if (typeof value === "string") { | ||
return value.trim().length > 0; | ||
} | ||
return value; | ||
return run(value, options, this); | ||
}.property(field); | ||
}; | ||
export { ValidationMixin, validate }; | ||
var validateEach = function(field, options) { | ||
return function() { | ||
return true; | ||
}.property().meta({validateEach: true, options: options}); | ||
}; | ||
export { ValidationMixin, validate, validateEach }; |
@@ -21,3 +21,3 @@ /* globals requirejs, require */ | ||
if (this.shouldLoadModule(moduleName)) { | ||
require(moduleName); | ||
this.require(moduleName); | ||
} | ||
@@ -28,2 +28,14 @@ } | ||
TestLoader.prototype.require = function(moduleName) { | ||
try { | ||
require(moduleName); | ||
} catch(e) { | ||
this.moduleLoadFailure(moduleName, e); | ||
} | ||
}; | ||
TestLoader.prototype.moduleLoadFailure = function(moduleName, error) { | ||
console.error('Error loading: ' + moduleName, error.stack); | ||
}; | ||
TestLoader.load = function() { | ||
@@ -30,0 +42,0 @@ new TestLoader().loadModules(); |
{ | ||
"name": "ember-cli-simple-validation", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "ember-cli addon that provides simple form validation", | ||
@@ -28,17 +28,18 @@ "directories": { | ||
"devDependencies": { | ||
"bower": "^1.3.12", | ||
"broccoli-asset-rev": "^2.0.0", | ||
"broccoli-ember-hbs-template-compiler": "^1.6.1", | ||
"ember-cli": "0.1.15", | ||
"ember-cli-6to5": "^3.0.0", | ||
"ember-cli": "0.2.0-beta.1", | ||
"ember-cli-app-version": "0.3.1", | ||
"ember-cli-babel": "^4.0.0", | ||
"ember-cli-content-security-policy": "0.3.0", | ||
"ember-cli-dependency-checker": "0.0.7", | ||
"ember-cli-htmlbars": "0.7.4", | ||
"ember-cli-inject-live-reload": "^1.3.0", | ||
"ember-cli-qunit": "0.3.7", | ||
"ember-cli-simple-store": "0.5.0", | ||
"ember-cli-app-version": "0.3.1", | ||
"ember-cli-qunit": "0.3.8", | ||
"ember-cli-simple-store": "^0.6.1", | ||
"ember-cli-uglify": "1.0.1", | ||
"ember-export-application-global": "^1.0.2", | ||
"express": "^4.8.5", | ||
"glob": "^4.0.5" | ||
"glob": "^4.0.5", | ||
"rimraf": "2.3.1" | ||
}, | ||
@@ -45,0 +46,0 @@ "keywords": [ |
@@ -97,2 +97,26 @@ # ember-cli-simple-validation | ||
## What about ArrayControllers? | ||
To validate each field on your ArrayController you would first declare the controller | ||
```js | ||
import Ember from "ember"; | ||
import {ValidationMixin, validateEach} from "ember-cli-simple-validation/mixins/validate"; | ||
export default Ember.ArrayController.extend(ValidationMixin, { | ||
name: validateEach("name") | ||
}); | ||
``` | ||
Next add the validation-error-field component to your templates each loop | ||
```js | ||
{{#each model as |person index|}} | ||
<div class="name-parent-div"> | ||
{{input value=person.name placeholder="name"}} | ||
{{#validation-error-field submitted=submitted field="name" model=person index=index}}invalid name{{/validation-error-field}} | ||
</div> | ||
{{/each}} | ||
``` | ||
## Running the unit tests | ||
@@ -99,0 +123,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9886532
249
120979
146
1
16