From a84587ab85ba8ee643a843451d4207993bfe0dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Macias?= Date: Tue, 12 May 2015 12:06:01 +0200 Subject: [PATCH] Move Validator service to view.validate() method --- .../ng-admin/Crud/form/FormController.js | 7 ++-- src/javascripts/ng-admin/Main/MainModule.js | 1 - .../Main/component/service/Validator.js | 30 --------------- src/javascripts/ng-admin/es6/lib/View/View.js | 9 +++++ .../ng-admin/es6/tests/lib/View/ViewTest.js | 26 +++++++++++++ .../Main/component/service/ValidatorSpec.js | 38 ------------------- 6 files changed, 38 insertions(+), 73 deletions(-) delete mode 100644 src/javascripts/ng-admin/Main/component/service/Validator.js delete mode 100644 src/javascripts/test/unit/Main/component/service/ValidatorSpec.js diff --git a/src/javascripts/ng-admin/Crud/form/FormController.js b/src/javascripts/ng-admin/Crud/form/FormController.js index 77e906fa..400b8543 100644 --- a/src/javascripts/ng-admin/Crud/form/FormController.js +++ b/src/javascripts/ng-admin/Crud/form/FormController.js @@ -3,7 +3,7 @@ define(function () { 'use strict'; - var FormController = function ($scope, $state, CreateQueries, UpdateQueries, Validator, Configuration, + var FormController = function ($scope, $state, CreateQueries, UpdateQueries, Configuration, progression, notification, view, dataStore) { this.$scope = $scope; @@ -11,7 +11,6 @@ define(function () { this.CreateQueries = CreateQueries; this.UpdateQueries = UpdateQueries; this.dataStore = dataStore; - this.Validator = Validator; this.progression = progression; this.notification = notification; this.title = view.title(); @@ -64,7 +63,7 @@ define(function () { ); try { - this.Validator.validate(this.view, mappedObject); + this.view.validate(mappedObject); } catch (e) { this.notification.log(e, {addnCls: 'humane-flatty-error'}); return false; @@ -134,7 +133,7 @@ define(function () { this.entity = undefined; }; - FormController.$inject = ['$scope', '$state', 'CreateQueries', 'UpdateQueries', 'Validator', 'NgAdminConfiguration', 'progression', 'notification', 'view', 'dataStore']; + FormController.$inject = ['$scope', '$state', 'CreateQueries', 'UpdateQueries', 'NgAdminConfiguration', 'progression', 'notification', 'view', 'dataStore']; return FormController; }); diff --git a/src/javascripts/ng-admin/Main/MainModule.js b/src/javascripts/ng-admin/Main/MainModule.js index 37f3a09e..356c7db1 100644 --- a/src/javascripts/ng-admin/Main/MainModule.js +++ b/src/javascripts/ng-admin/Main/MainModule.js @@ -14,7 +14,6 @@ define(function (require) { MainModule.controller('DashboardController', require('ng-admin/Main/component/controller/DashboardController')); MainModule.service('PanelBuilder', require('ng-admin/Main/component/service/PanelBuilder')); - MainModule.service('Validator', require('ng-admin/Main/component/service/Validator')); MainModule.provider('NgAdminConfiguration', require('ng-admin/Main/component/provider/NgAdminConfiguration')); diff --git a/src/javascripts/ng-admin/Main/component/service/Validator.js b/src/javascripts/ng-admin/Main/component/service/Validator.js deleted file mode 100644 index f9c45a6c..00000000 --- a/src/javascripts/ng-admin/Main/component/service/Validator.js +++ /dev/null @@ -1,30 +0,0 @@ -/*global define*/ - -define(function () { - 'use strict'; - - function Validator() { - } - - /** - * Validate views fields - * - * @param {View} view - * @param {Entry} entry - * - * @returns {boolean} - */ - Validator.prototype.validate = function (view, entry) { - - view.getFields().forEach(function (field) { - var validation = field.validation(); - if (typeof (validation.validator) === 'function') { - validation.validator(entry.values[field.name()]); - } - }); - }; - - Validator.$inject = []; - - return Validator; -}); diff --git a/src/javascripts/ng-admin/es6/lib/View/View.js b/src/javascripts/ng-admin/es6/lib/View/View.js index 40ff9c0e..bf93e1a9 100644 --- a/src/javascripts/ng-admin/es6/lib/View/View.js +++ b/src/javascripts/ng-admin/es6/lib/View/View.js @@ -240,6 +240,15 @@ class View { return this._url; } + + validate(entry) { + this._fields.map(function (field) { + var validation = field.validation(); + if (typeof validation.validator === 'function') { + validation.validator(entry.values[field.name()]); + } + }); + } } export default View; diff --git a/src/javascripts/ng-admin/es6/tests/lib/View/ViewTest.js b/src/javascripts/ng-admin/es6/tests/lib/View/ViewTest.js index 0ad43b77..be89cafc 100644 --- a/src/javascripts/ng-admin/es6/tests/lib/View/ViewTest.js +++ b/src/javascripts/ng-admin/es6/tests/lib/View/ViewTest.js @@ -1,6 +1,7 @@ var assert = require('chai').assert; import Entity from "../../../lib/Entity/Entity"; +import Entry from "../../../lib/Entry"; import Field from "../../../lib/Field/Field"; import ReferenceField from "../../../lib/Field/ReferenceField"; import ReferenceManyField from "../../../lib/Field/ReferenceManyField"; @@ -162,6 +163,31 @@ describe('View', function() { }); }); + describe("validate()", function () { + it('should call validator on each fields.', function () { + var entry = new Entry(), + view = new View('myView'), + field1 = new Field('notValidable').label('Complex'), + field2 = new Field('simple').label('Simple'); + + entry.values = { + notValidable: false, + simple: 1 + }; + + view.addField(field1).addField(field2); + + field1.validation().validator = function () { + throw new Error('Field "Complex" is not valid.'); + }; + field2.validation().validator = function () { + return true; + }; + + assert.throw(function () { view.validate(entry); }, Error, 'Field "Complex" is not valid.'); + }); + }); + it('should return the identifier.', function () { var view = new View(new Entity('post')); view diff --git a/src/javascripts/test/unit/Main/component/service/ValidatorSpec.js b/src/javascripts/test/unit/Main/component/service/ValidatorSpec.js deleted file mode 100644 index a94312e3..00000000 --- a/src/javascripts/test/unit/Main/component/service/ValidatorSpec.js +++ /dev/null @@ -1,38 +0,0 @@ -/*global define,jasmine,angular,describe,it,expect*/ - -define(function (require) { - 'use strict'; - - var Validator = require('ng-admin/Main/component/service/Validator'), - View = require('ng-admin/es6/lib/View/View'), - Entry = require('ng-admin/es6/lib/Entry'), - Field = require('ng-admin/es6/lib/Field/Field'); - - describe("Service: Validator", function () { - - it('should call validator on each fields.', function () { - var validator = new Validator(), - entry = new Entry(), - view = new View('myView'), - field1 = new Field('notValidable').label('Complex'), - field2 = new Field('simple').label('Simple'); - - entry.values = { - notValidable: false, - simple: 1 - }; - - view.addField(field1).addField(field2); - - field1.validation().validator = function () { - throw new Error('Field "Complex" is not valid.'); - }; - field2.validation().validator = function () { - return true; - }; - - expect(function () { validator.validate(view, entry); }).toThrow(new Error('Field "Complex" is not valid.')); - }); - - }); -});