Skip to content

Commit

Permalink
Merge pull request #430 from marmelab/remove_validator_service
Browse files Browse the repository at this point in the history
[RFR] Move Validator service to view.validate() method
  • Loading branch information
manuquentin committed May 12, 2015
2 parents 1f4a4cc + a84587a commit e0da9fa
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 73 deletions.
7 changes: 3 additions & 4 deletions src/javascripts/ng-admin/Crud/form/FormController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
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;
this.$state = $state;
this.CreateQueries = CreateQueries;
this.UpdateQueries = UpdateQueries;
this.dataStore = dataStore;
this.Validator = Validator;
this.progression = progression;
this.notification = notification;
this.title = view.title();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
1 change: 0 additions & 1 deletion src/javascripts/ng-admin/Main/MainModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
30 changes: 0 additions & 30 deletions src/javascripts/ng-admin/Main/component/service/Validator.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/javascripts/ng-admin/es6/lib/View/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
26 changes: 26 additions & 0 deletions src/javascripts/ng-admin/es6/tests/lib/View/ViewTest.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down
38 changes: 0 additions & 38 deletions src/javascripts/test/unit/Main/component/service/ValidatorSpec.js

This file was deleted.

0 comments on commit e0da9fa

Please sign in to comment.