2017-01-31 13:13:06 +00:00
|
|
|
import {module} from './module';
|
2017-02-02 09:52:09 +00:00
|
|
|
import {validator} from 'vendor';
|
|
|
|
console.log(validator);
|
2017-02-01 17:55:02 +00:00
|
|
|
directive.$inject = ['$interpolate', '$compile', '$window']
|
|
|
|
export function directive(interpolate, compile, $window) {
|
2017-01-31 13:13:06 +00:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
require: ['ngModel', '^^form'],
|
|
|
|
link: function (scope, element, attrs, ctrl) {
|
2017-02-01 17:55:02 +00:00
|
|
|
let vnValidations = $window.validations;
|
|
|
|
|
|
|
|
if(!attrs['vnValidation'])
|
|
|
|
return;
|
|
|
|
|
|
|
|
let split = attrs['vnValidation'].split('.');
|
|
|
|
|
|
|
|
if(split.length != 2)
|
|
|
|
throw new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`);
|
|
|
|
|
|
|
|
let entityName = firstUpper(split[0]);
|
|
|
|
let fieldName = split[1];
|
|
|
|
let entity = vnValidations[entityName];
|
|
|
|
|
|
|
|
if(!entity)
|
|
|
|
throw new Error(`vnValidation: Entity '${entityName}' doesn't exist`);
|
|
|
|
|
|
|
|
let validations = entity.validations[fieldName];
|
|
|
|
|
|
|
|
if(!validations)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let input = ctrl[0],
|
2017-01-31 13:13:06 +00:00
|
|
|
form = ctrl[1],
|
|
|
|
messages = [],
|
|
|
|
customValidator = {},
|
|
|
|
parentMessage;
|
|
|
|
|
2017-02-01 17:55:02 +00:00
|
|
|
let i = 0;
|
|
|
|
for(let conf of validations){
|
|
|
|
let key = `v${i++}`;
|
|
|
|
|
|
|
|
let messageNode = createMessage(form.$name, input.$name, key);
|
|
|
|
customValidator[key] = messageNode;
|
|
|
|
messages.push(messageNode);
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-02-01 17:55:02 +00:00
|
|
|
input.$validators[key] = function(value) {
|
|
|
|
return isValid(value, conf, messageNode);
|
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
2017-02-01 17:55:02 +00:00
|
|
|
|
|
|
|
if(messages.length > 0) {
|
|
|
|
parentMessage = angular.element(createMessages(form.$name, input.$name));
|
|
|
|
messages.forEach(function (item) {
|
|
|
|
parentMessage.append(item);
|
|
|
|
});
|
|
|
|
messages = null;
|
|
|
|
element.after(compile(parentMessage)(scope));
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
2017-02-01 17:55:02 +00:00
|
|
|
|
|
|
|
scope.$on('destroy', function() {
|
|
|
|
customValidator = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function firstUpper(str) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.substr(1);
|
|
|
|
}
|
|
|
|
function createMessage(form, input, key) {
|
|
|
|
var template = '<span class="mdl-textfield__error" ng-show="{{form}}.$dirty && {{form}}.{{input}}.$error.{{key}}"></span>';
|
|
|
|
var span = interpolate(template)({ form: form, input: input, key: key });
|
|
|
|
var element = angular.element(span);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
function createMessages(form, input) {
|
|
|
|
var template = '<div ng-show="{{form}}.$dirty && {{form}}.{{input}}.$invalid"></div>';
|
|
|
|
return interpolate(template)({ form: form, input: input });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loopback code with modifications
|
|
|
|
|
|
|
|
function isValid(value, conf, messageNode) {
|
|
|
|
let valid = true;
|
|
|
|
let validator = validators[conf.validation];
|
|
|
|
|
2017-02-02 09:52:09 +00:00
|
|
|
try {
|
|
|
|
nullCheck(value, conf);
|
|
|
|
validator && validator(value, conf);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
let message = conf.message ? conf.message : e.message;
|
|
|
|
messageNode.text(message);
|
|
|
|
return false;
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-02-02 09:52:09 +00:00
|
|
|
return true;
|
2017-02-01 17:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.directive('vnValidation', directive);
|
|
|
|
|
|
|
|
var validators = {
|
2017-02-02 09:52:09 +00:00
|
|
|
presence: function(value, conf) {
|
|
|
|
console.log(value);
|
|
|
|
if(validator.isEmpty(value))
|
|
|
|
throw new Error(`Value can't be empty`);
|
|
|
|
},
|
|
|
|
length: function(value, conf) {
|
|
|
|
let options = {
|
|
|
|
min: conf.min || conf.is,
|
|
|
|
max: conf.max || conf.is
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!validator.isLength(value, options)) {
|
|
|
|
if(conf.is)
|
|
|
|
throw new Error(`Value shoud be ${conf.is} characters long`);
|
|
|
|
else
|
|
|
|
throw new Error(`Value shoud have a length between ${conf.min} and ${conf.max}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
numericality: function(value, conf) {
|
|
|
|
if(conf.int) {
|
|
|
|
if(!validator.isInt(value))
|
|
|
|
throw new Error(`Value shoud be integer`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(!validator.isNumeric(vlaue))
|
|
|
|
throw new Error(`Value shoud be a number`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
inclusion: function(value, conf) {
|
|
|
|
if(!validator.isIn(value, conf.in))
|
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
exclusion: function(value, conf) {
|
|
|
|
if(validator.isIn(value, conf.in))
|
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
format: function(value, conf) {
|
|
|
|
if(!validator.matches(value, conf.with))
|
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
custom: function(value, conf) {
|
|
|
|
let valid = true;
|
|
|
|
function err(kind) {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let inst = {attr: value};
|
|
|
|
conf.customValidator.call(inst, err)
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-02-02 09:52:09 +00:00
|
|
|
if(!valid)
|
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
absence: function() {},
|
|
|
|
uniqueness: function() {},
|
2017-02-01 17:55:02 +00:00
|
|
|
};
|
|
|
|
|
2017-02-02 09:52:09 +00:00
|
|
|
function nullCheck(value, conf) {
|
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
if (!conf.allowBlank)
|
|
|
|
throw new Error(`Value can't be blank`);
|
|
|
|
} else {
|
|
|
|
if (value === null && !conf.allowNull)
|
|
|
|
throw new Error(`Value can't be null`);
|
2017-02-01 17:55:02 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|