knockout.validation.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /*=============================================================================
  2. Author: Eric M. Barnard - @ericmbarnard
  3. License: MIT (http://opensource.org/licenses/mit-license.php)
  4. Description: Validation Library for KnockoutJS
  5. Version: 2.0.2
  6. ===============================================================================
  7. */
  8. /*globals require: false, exports: false, define: false, ko: false */
  9. (function (factory) {
  10. // Module systems magic dance.
  11. if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
  12. // CommonJS or Node: hard-coded dependency on "knockout"
  13. factory(require("knockout"), exports);
  14. } else if (typeof define === "function" && define["amd"]) {
  15. // AMD anonymous module with hard-coded dependency on "knockout"
  16. define(["knockout", "exports"], factory);
  17. } else {
  18. // <script> tag: use the global `ko` object, attaching a `mapping` property
  19. factory(ko, ko.validation = {});
  20. }
  21. }(function ( ko, exports ) {
  22. if (typeof (ko) === 'undefined') {
  23. throw new Error('Knockout is required, please ensure it is loaded before loading this validation plug-in');
  24. }
  25. // create our namespace object
  26. ko.validation = exports;
  27. var kv = ko.validation,
  28. koUtils = ko.utils,
  29. unwrap = koUtils.unwrapObservable,
  30. forEach = koUtils.arrayForEach,
  31. extend = koUtils.extend;
  32. ;/*global ko: false*/
  33. var defaults = {
  34. registerExtenders: true,
  35. messagesOnModified: true,
  36. errorsAsTitle: true, // enables/disables showing of errors as title attribute of the target element.
  37. errorsAsTitleOnModified: false, // shows the error when hovering the input field (decorateElement must be true)
  38. messageTemplate: null,
  39. insertMessages: true, // automatically inserts validation messages as <span></span>
  40. parseInputAttributes: false, // parses the HTML5 validation attribute from a form element and adds that to the object
  41. writeInputAttributes: false, // adds HTML5 input validation attributes to form elements that ko observable's are bound to
  42. decorateInputElement: false, // false to keep backward compatibility
  43. decorateElementOnModified: true,// true to keep backward compatibility
  44. errorClass: null, // single class for error message and element
  45. errorElementClass: 'validationElement', // class to decorate error element
  46. errorMessageClass: 'validationMessage', // class to decorate error message
  47. allowHtmlMessages: false, // allows HTML in validation messages
  48. grouping: {
  49. deep: false, //by default grouping is shallow
  50. observable: true, //and using observables
  51. live: false //react to changes to observableArrays if observable === true
  52. },
  53. validate: {
  54. // throttle: 10
  55. }
  56. };
  57. // make a copy so we can use 'reset' later
  58. var configuration = extend({}, defaults);
  59. configuration.html5Attributes = ['required', 'pattern', 'min', 'max', 'step'];
  60. configuration.html5InputTypes = ['email', 'number', 'date'];
  61. configuration.reset = function () {
  62. extend(configuration, defaults);
  63. };
  64. kv.configuration = configuration;
  65. ;kv.utils = (function () {
  66. var seedId = new Date().getTime();
  67. var domData = {}; //hash of data objects that we reference from dom elements
  68. var domDataKey = '__ko_validation__';
  69. return {
  70. isArray: function (o) {
  71. return o.isArray || Object.prototype.toString.call(o) === '[object Array]';
  72. },
  73. isObject: function (o) {
  74. return o !== null && typeof o === 'object';
  75. },
  76. isNumber: function(o) {
  77. return !isNaN(o);
  78. },
  79. isObservableArray: function(instance) {
  80. return !!instance &&
  81. typeof instance["remove"] === "function" &&
  82. typeof instance["removeAll"] === "function" &&
  83. typeof instance["destroy"] === "function" &&
  84. typeof instance["destroyAll"] === "function" &&
  85. typeof instance["indexOf"] === "function" &&
  86. typeof instance["replace"] === "function";
  87. },
  88. values: function (o) {
  89. var r = [];
  90. for (var i in o) {
  91. if (o.hasOwnProperty(i)) {
  92. r.push(o[i]);
  93. }
  94. }
  95. return r;
  96. },
  97. getValue: function (o) {
  98. return (typeof o === 'function' ? o() : o);
  99. },
  100. hasAttribute: function (node, attr) {
  101. return node.getAttribute(attr) !== null;
  102. },
  103. getAttribute: function (element, attr) {
  104. return element.getAttribute(attr);
  105. },
  106. setAttribute: function (element, attr, value) {
  107. return element.setAttribute(attr, value);
  108. },
  109. isValidatable: function (o) {
  110. return !!(o && o.rules && o.isValid && o.isModified);
  111. },
  112. insertAfter: function (node, newNode) {
  113. node.parentNode.insertBefore(newNode, node.nextSibling);
  114. },
  115. newId: function () {
  116. return seedId += 1;
  117. },
  118. getConfigOptions: function (element) {
  119. var options = kv.utils.contextFor(element);
  120. return options || kv.configuration;
  121. },
  122. setDomData: function (node, data) {
  123. var key = node[domDataKey];
  124. if (!key) {
  125. node[domDataKey] = key = kv.utils.newId();
  126. }
  127. domData[key] = data;
  128. },
  129. getDomData: function (node) {
  130. var key = node[domDataKey];
  131. if (!key) {
  132. return undefined;
  133. }
  134. return domData[key];
  135. },
  136. contextFor: function (node) {
  137. switch (node.nodeType) {
  138. case 1:
  139. case 8:
  140. var context = kv.utils.getDomData(node);
  141. if (context) { return context; }
  142. if (node.parentNode) { return kv.utils.contextFor(node.parentNode); }
  143. break;
  144. }
  145. return undefined;
  146. },
  147. isEmptyVal: function (val) {
  148. if (val === undefined) {
  149. return true;
  150. }
  151. if (val === null) {
  152. return true;
  153. }
  154. if (val === "") {
  155. return true;
  156. }
  157. },
  158. getOriginalElementTitle: function (element) {
  159. var savedOriginalTitle = kv.utils.getAttribute(element, 'data-orig-title'),
  160. currentTitle = element.title,
  161. hasSavedOriginalTitle = kv.utils.hasAttribute(element, 'data-orig-title');
  162. return hasSavedOriginalTitle ?
  163. savedOriginalTitle : currentTitle;
  164. },
  165. async: function (expr) {
  166. if (window.setImmediate) { window.setImmediate(expr); }
  167. else { window.setTimeout(expr, 0); }
  168. },
  169. forEach: function (object, callback) {
  170. if (kv.utils.isArray(object)) {
  171. return forEach(object, callback);
  172. }
  173. for (var prop in object) {
  174. if (object.hasOwnProperty(prop)) {
  175. callback(object[prop], prop);
  176. }
  177. }
  178. }
  179. };
  180. }());;var api = (function () {
  181. var isInitialized = 0,
  182. configuration = kv.configuration,
  183. utils = kv.utils;
  184. function cleanUpSubscriptions(context) {
  185. forEach(context.subscriptions, function (subscription) {
  186. subscription.dispose();
  187. });
  188. context.subscriptions = [];
  189. }
  190. function dispose(context) {
  191. if (context.options.deep) {
  192. forEach(context.flagged, function (obj) {
  193. delete obj.__kv_traversed;
  194. });
  195. context.flagged.length = 0;
  196. }
  197. if (!context.options.live) {
  198. cleanUpSubscriptions(context);
  199. }
  200. }
  201. function runTraversal(obj, context) {
  202. context.validatables = [];
  203. cleanUpSubscriptions(context);
  204. traverseGraph(obj, context);
  205. dispose(context);
  206. }
  207. function traverseGraph(obj, context, level) {
  208. var objValues = [],
  209. val = obj.peek ? obj.peek() : obj;
  210. if (obj.__kv_traversed === true) {
  211. return;
  212. }
  213. if (context.options.deep) {
  214. obj.__kv_traversed = true;
  215. context.flagged.push(obj);
  216. }
  217. //default level value depends on deep option.
  218. level = (level !== undefined ? level : context.options.deep ? 1 : -1);
  219. // if object is observable then add it to the list
  220. if (ko.isObservable(obj)) {
  221. // ensure it's validatable but don't extend validatedObservable because it
  222. // would overwrite isValid property.
  223. if (!obj.errors && !utils.isValidatable(obj)) {
  224. obj.extend({ validatable: true });
  225. }
  226. context.validatables.push(obj);
  227. if (context.options.live && utils.isObservableArray(obj)) {
  228. context.subscriptions.push(obj.subscribe(function () {
  229. context.graphMonitor.valueHasMutated();
  230. }));
  231. }
  232. }
  233. //get list of values either from array or object but ignore non-objects
  234. // and destroyed objects
  235. if (val && !val._destroy) {
  236. if (utils.isArray(val)) {
  237. objValues = val;
  238. }
  239. else if (utils.isObject(val)) {
  240. objValues = utils.values(val);
  241. }
  242. }
  243. //process recursively if it is deep grouping
  244. if (level !== 0) {
  245. utils.forEach(objValues, function (observable) {
  246. //but not falsy things and not HTML Elements
  247. if (observable && !observable.nodeType && (!ko.isComputed(observable) || observable.rules)) {
  248. traverseGraph(observable, context, level + 1);
  249. }
  250. });
  251. }
  252. }
  253. function collectErrors(array) {
  254. var errors = [];
  255. forEach(array, function (observable) {
  256. // Do not collect validatedObservable errors
  257. if (utils.isValidatable(observable) && !observable.isValid()) {
  258. // Use peek because we don't want a dependency for 'error' property because it
  259. // changes before 'isValid' does. (Issue #99)
  260. errors.push(observable.error.peek());
  261. }
  262. });
  263. return errors;
  264. }
  265. return {
  266. //Call this on startup
  267. //any config can be overridden with the passed in options
  268. init: function (options, force) {
  269. //done run this multiple times if we don't really want to
  270. if (isInitialized > 0 && !force) {
  271. return;
  272. }
  273. //because we will be accessing options properties it has to be an object at least
  274. options = options || {};
  275. //if specific error classes are not provided then apply generic errorClass
  276. //it has to be done on option so that options.errorClass can override default
  277. //errorElementClass and errorMessage class but not those provided in options
  278. options.errorElementClass = options.errorElementClass || options.errorClass || configuration.errorElementClass;
  279. options.errorMessageClass = options.errorMessageClass || options.errorClass || configuration.errorMessageClass;
  280. extend(configuration, options);
  281. if (configuration.registerExtenders) {
  282. kv.registerExtenders();
  283. }
  284. isInitialized = 1;
  285. },
  286. // resets the config back to its original state
  287. reset: kv.configuration.reset,
  288. // recursively walks a viewModel and creates an object that
  289. // provides validation information for the entire viewModel
  290. // obj -> the viewModel to walk
  291. // options -> {
  292. // deep: false, // if true, will walk past the first level of viewModel properties
  293. // observable: false // if true, returns a computed observable indicating if the viewModel is valid
  294. // }
  295. group: function group(obj, options) { // array of observables or viewModel
  296. options = extend(extend({}, configuration.grouping), options);
  297. var context = {
  298. options: options,
  299. graphMonitor: ko.observable(),
  300. flagged: [],
  301. subscriptions: [],
  302. validatables: []
  303. };
  304. var result = null;
  305. //if using observables then traverse structure once and add observables
  306. if (options.observable) {
  307. result = ko.computed(function () {
  308. context.graphMonitor(); //register dependency
  309. runTraversal(obj, context);
  310. return collectErrors(context.validatables);
  311. });
  312. }
  313. else { //if not using observables then every call to error() should traverse the structure
  314. result = function () {
  315. runTraversal(obj, context);
  316. return collectErrors(context.validatables);
  317. };
  318. }
  319. result.showAllMessages = function (show) { // thanks @heliosPortal
  320. if (show === undefined) {//default to true
  321. show = true;
  322. }
  323. result.forEach(function (observable) {
  324. if (utils.isValidatable(observable)) {
  325. observable.isModified(show);
  326. }
  327. });
  328. };
  329. result.isAnyMessageShown = function () {
  330. var invalidAndModifiedPresent;
  331. invalidAndModifiedPresent = !!result.find(function (observable) {
  332. return utils.isValidatable(observable) && !observable.isValid() && observable.isModified();
  333. });
  334. return invalidAndModifiedPresent;
  335. };
  336. result.filter = function(predicate) {
  337. predicate = predicate || function () { return true; };
  338. // ensure we have latest changes
  339. result();
  340. return koUtils.arrayFilter(context.validatables, predicate);
  341. };
  342. result.find = function(predicate) {
  343. predicate = predicate || function () { return true; };
  344. // ensure we have latest changes
  345. result();
  346. return koUtils.arrayFirst(context.validatables, predicate);
  347. };
  348. result.forEach = function(callback) {
  349. callback = callback || function () { };
  350. // ensure we have latest changes
  351. result();
  352. forEach(context.validatables, callback);
  353. };
  354. result.map = function(mapping) {
  355. mapping = mapping || function (item) { return item; };
  356. // ensure we have latest changes
  357. result();
  358. return koUtils.arrayMap(context.validatables, mapping);
  359. };
  360. /**
  361. * @private You should not rely on this method being here.
  362. * It's a private method and it may change in the future.
  363. *
  364. * @description Updates the validated object and collects errors from it.
  365. */
  366. result._updateState = function(newValue) {
  367. if (!utils.isObject(newValue)) {
  368. throw new Error('An object is required.');
  369. }
  370. obj = newValue;
  371. if (options.observable) {
  372. context.graphMonitor.valueHasMutated();
  373. }
  374. else {
  375. runTraversal(newValue, context);
  376. return collectErrors(context.validatables);
  377. }
  378. };
  379. return result;
  380. },
  381. formatMessage: function (message, params, observable) {
  382. if (utils.isObject(params) && params.typeAttr) {
  383. params = params.value;
  384. }
  385. if (typeof (message) === 'function') {
  386. return message(params, observable);
  387. }
  388. var replacements = unwrap(params) || [];
  389. if (!utils.isArray(replacements)) {
  390. replacements = [replacements];
  391. }
  392. return message.replace(/{(\d+)}/gi, function(match, index) {
  393. if (typeof replacements[index] !== 'undefined') {
  394. return replacements[index];
  395. }
  396. return match;
  397. });
  398. },
  399. // addRule:
  400. // This takes in a ko.observable and a Rule Context - which is just a rule name and params to supply to the validator
  401. // ie: kv.addRule(myObservable, {
  402. // rule: 'required',
  403. // params: true
  404. // });
  405. //
  406. addRule: function (observable, rule) {
  407. observable.extend({ validatable: true });
  408. var hasRule = !!koUtils.arrayFirst(observable.rules(), function(item) {
  409. return item.rule && item.rule === rule.rule;
  410. });
  411. if (!hasRule) {
  412. //push a Rule Context to the observables local array of Rule Contexts
  413. observable.rules.push(rule);
  414. }
  415. return observable;
  416. },
  417. // addAnonymousRule:
  418. // Anonymous Rules essentially have all the properties of a Rule, but are only specific for a certain property
  419. // and developers typically are wanting to add them on the fly or not register a rule with the 'kv.rules' object
  420. //
  421. // Example:
  422. // var test = ko.observable('something').extend{(
  423. // validation: {
  424. // validator: function(val, someOtherVal){
  425. // return true;
  426. // },
  427. // message: "Something must be really wrong!',
  428. // params: true
  429. // }
  430. // )};
  431. addAnonymousRule: function (observable, ruleObj) {
  432. if (ruleObj['message'] === undefined) {
  433. ruleObj['message'] = 'Error';
  434. }
  435. //make sure onlyIf is honoured
  436. if (ruleObj.onlyIf) {
  437. ruleObj.condition = ruleObj.onlyIf;
  438. }
  439. //add the anonymous rule to the observable
  440. kv.addRule(observable, ruleObj);
  441. },
  442. addExtender: function (ruleName) {
  443. ko.extenders[ruleName] = function (observable, params) {
  444. //params can come in a few flavors
  445. // 1. Just the params to be passed to the validator
  446. // 2. An object containing the Message to be used and the Params to pass to the validator
  447. // 3. A condition when the validation rule to be applied
  448. //
  449. // Example:
  450. // var test = ko.observable(3).extend({
  451. // max: {
  452. // message: 'This special field has a Max of {0}',
  453. // params: 2,
  454. // onlyIf: function() {
  455. // return specialField.IsVisible();
  456. // }
  457. // }
  458. // )};
  459. //
  460. if (params && (params.message || params.onlyIf)) { //if it has a message or condition object, then its an object literal to use
  461. return kv.addRule(observable, {
  462. rule: ruleName,
  463. message: params.message,
  464. params: utils.isEmptyVal(params.params) ? true : params.params,
  465. condition: params.onlyIf
  466. });
  467. } else {
  468. return kv.addRule(observable, {
  469. rule: ruleName,
  470. params: params
  471. });
  472. }
  473. };
  474. },
  475. // loops through all kv.rules and adds them as extenders to
  476. // ko.extenders
  477. registerExtenders: function () { // root extenders optional, use 'validation' extender if would cause conflicts
  478. if (configuration.registerExtenders) {
  479. for (var ruleName in kv.rules) {
  480. if (kv.rules.hasOwnProperty(ruleName)) {
  481. if (!ko.extenders[ruleName]) {
  482. kv.addExtender(ruleName);
  483. }
  484. }
  485. }
  486. }
  487. },
  488. //creates a span next to the @element with the specified error class
  489. insertValidationMessage: function (element) {
  490. var span = document.createElement('SPAN');
  491. span.className = utils.getConfigOptions(element).errorMessageClass;
  492. utils.insertAfter(element, span);
  493. return span;
  494. },
  495. // if html-5 validation attributes have been specified, this parses
  496. // the attributes on @element
  497. parseInputValidationAttributes: function (element, valueAccessor) {
  498. forEach(kv.configuration.html5Attributes, function (attr) {
  499. if (utils.hasAttribute(element, attr)) {
  500. var params = element.getAttribute(attr) || true;
  501. if (attr === 'min' || attr === 'max')
  502. {
  503. // If we're validating based on the min and max attributes, we'll
  504. // need to know what the 'type' attribute is set to
  505. var typeAttr = element.getAttribute('type');
  506. if (typeof typeAttr === "undefined" || !typeAttr)
  507. {
  508. // From http://www.w3.org/TR/html-markup/input:
  509. // An input element with no type attribute specified represents the
  510. // same thing as an input element with its type attribute set to "text".
  511. typeAttr = "text";
  512. }
  513. params = {typeAttr: typeAttr, value: params};
  514. }
  515. kv.addRule(valueAccessor(), {
  516. rule: attr,
  517. params: params
  518. });
  519. }
  520. });
  521. var currentType = element.getAttribute('type');
  522. forEach(kv.configuration.html5InputTypes, function (type) {
  523. if (type === currentType) {
  524. kv.addRule(valueAccessor(), {
  525. rule: (type === 'date') ? 'dateISO' : type,
  526. params: true
  527. });
  528. }
  529. });
  530. },
  531. // writes html5 validation attributes on the element passed in
  532. writeInputValidationAttributes: function (element, valueAccessor) {
  533. var observable = valueAccessor();
  534. if (!observable || !observable.rules) {
  535. return;
  536. }
  537. var contexts = observable.rules(); // observable array
  538. // loop through the attributes and add the information needed
  539. forEach(kv.configuration.html5Attributes, function (attr) {
  540. var ctx = koUtils.arrayFirst(contexts, function (ctx) {
  541. return ctx.rule && ctx.rule.toLowerCase() === attr.toLowerCase();
  542. });
  543. if (!ctx) {
  544. return;
  545. }
  546. // we have a rule matching a validation attribute at this point
  547. // so lets add it to the element along with the params
  548. ko.computed({
  549. read: function() {
  550. var params = ko.unwrap(ctx.params);
  551. // we have to do some special things for the pattern validation
  552. if (ctx.rule === "pattern" && params instanceof RegExp) {
  553. // we need the pure string representation of the RegExpr without the //gi stuff
  554. params = params.source;
  555. }
  556. element.setAttribute(attr, params);
  557. },
  558. disposeWhenNodeIsRemoved: element
  559. });
  560. });
  561. contexts = null;
  562. },
  563. //take an existing binding handler and make it cause automatic validations
  564. makeBindingHandlerValidatable: function (handlerName) {
  565. var init = ko.bindingHandlers[handlerName].init;
  566. ko.bindingHandlers[handlerName].init = function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  567. init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
  568. return ko.bindingHandlers['validationCore'].init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
  569. };
  570. },
  571. // visit an objects properties and apply validation rules from a definition
  572. setRules: function (target, definition) {
  573. var setRules = function (target, definition) {
  574. if (!target || !definition) { return; }
  575. for (var prop in definition) {
  576. if (!definition.hasOwnProperty(prop)) { continue; }
  577. var ruleDefinitions = definition[prop];
  578. //check the target property exists and has a value
  579. if (!target[prop]) { continue; }
  580. var targetValue = target[prop],
  581. unwrappedTargetValue = unwrap(targetValue),
  582. rules = {},
  583. nonRules = {};
  584. for (var rule in ruleDefinitions) {
  585. if (!ruleDefinitions.hasOwnProperty(rule)) { continue; }
  586. if (kv.rules[rule]) {
  587. rules[rule] = ruleDefinitions[rule];
  588. } else {
  589. nonRules[rule] = ruleDefinitions[rule];
  590. }
  591. }
  592. //apply rules
  593. if (ko.isObservable(targetValue)) {
  594. targetValue.extend(rules);
  595. }
  596. //then apply child rules
  597. //if it's an array, apply rules to all children
  598. if (unwrappedTargetValue && utils.isArray(unwrappedTargetValue)) {
  599. for (var i = 0; i < unwrappedTargetValue.length; i++) {
  600. setRules(unwrappedTargetValue[i], nonRules);
  601. }
  602. //otherwise, just apply to this property
  603. } else {
  604. setRules(unwrappedTargetValue, nonRules);
  605. }
  606. }
  607. };
  608. setRules(target, definition);
  609. }
  610. };
  611. }());
  612. // expose api publicly
  613. extend(ko.validation, api);
  614. ;//Validation Rules:
  615. // You can view and override messages or rules via:
  616. // kv.rules[ruleName]
  617. //
  618. // To implement a custom Rule, simply use this template:
  619. // kv.rules['<custom rule name>'] = {
  620. // validator: function (val, param) {
  621. // <custom logic>
  622. // return <true or false>;
  623. // },
  624. // message: '<custom validation message>' //optionally you can also use a '{0}' to denote a placeholder that will be replaced with your 'param'
  625. // };
  626. //
  627. // Example:
  628. // kv.rules['mustEqual'] = {
  629. // validator: function( val, mustEqualVal ){
  630. // return val === mustEqualVal;
  631. // },
  632. // message: 'This field must equal {0}'
  633. // };
  634. //
  635. kv.rules = {};
  636. kv.rules['required'] = {
  637. validator: function (val, required) {
  638. var testVal;
  639. if (val === undefined || val === null) {
  640. return !required;
  641. }
  642. testVal = val;
  643. if (typeof (val) === 'string') {
  644. if (String.prototype.trim) {
  645. testVal = val.trim();
  646. }
  647. else {
  648. testVal = val.replace(/^\s+|\s+$/g, '');
  649. }
  650. }
  651. if (!required) {// if they passed: { required: false }, then don't require this
  652. return true;
  653. }
  654. return ((testVal + '').length > 0);
  655. },
  656. message: 'This field is required.'
  657. };
  658. function minMaxValidatorFactory(validatorName) {
  659. var isMaxValidation = validatorName === "max";
  660. return function (val, options) {
  661. if (kv.utils.isEmptyVal(val)) {
  662. return true;
  663. }
  664. var comparisonValue, type;
  665. if (options.typeAttr === undefined) {
  666. // This validator is being called from javascript rather than
  667. // being bound from markup
  668. type = "text";
  669. comparisonValue = options;
  670. } else {
  671. type = options.typeAttr;
  672. comparisonValue = options.value;
  673. }
  674. // From http://www.w3.org/TR/2012/WD-html5-20121025/common-input-element-attributes.html#attr-input-min,
  675. // if the value is parseable to a number, then the minimum should be numeric
  676. if (!isNaN(comparisonValue) && !(comparisonValue instanceof Date)) {
  677. type = "number";
  678. }
  679. var regex, valMatches, comparisonValueMatches;
  680. switch (type.toLowerCase()) {
  681. case "week":
  682. regex = /^(\d{4})-W(\d{2})$/;
  683. valMatches = val.match(regex);
  684. if (valMatches === null) {
  685. throw new Error("Invalid value for " + validatorName + " attribute for week input. Should look like " +
  686. "'2000-W33' http://www.w3.org/TR/html-markup/input.week.html#input.week.attrs.min");
  687. }
  688. comparisonValueMatches = comparisonValue.match(regex);
  689. // If no regex matches were found, validation fails
  690. if (!comparisonValueMatches) {
  691. return false;
  692. }
  693. if (isMaxValidation) {
  694. return (valMatches[1] < comparisonValueMatches[1]) || // older year
  695. // same year, older week
  696. ((valMatches[1] === comparisonValueMatches[1]) && (valMatches[2] <= comparisonValueMatches[2]));
  697. } else {
  698. return (valMatches[1] > comparisonValueMatches[1]) || // newer year
  699. // same year, newer week
  700. ((valMatches[1] === comparisonValueMatches[1]) && (valMatches[2] >= comparisonValueMatches[2]));
  701. }
  702. break;
  703. case "month":
  704. regex = /^(\d{4})-(\d{2})$/;
  705. valMatches = val.match(regex);
  706. if (valMatches === null) {
  707. throw new Error("Invalid value for " + validatorName + " attribute for month input. Should look like " +
  708. "'2000-03' http://www.w3.org/TR/html-markup/input.month.html#input.month.attrs.min");
  709. }
  710. comparisonValueMatches = comparisonValue.match(regex);
  711. // If no regex matches were found, validation fails
  712. if (!comparisonValueMatches) {
  713. return false;
  714. }
  715. if (isMaxValidation) {
  716. return ((valMatches[1] < comparisonValueMatches[1]) || // older year
  717. // same year, older month
  718. ((valMatches[1] === comparisonValueMatches[1]) && (valMatches[2] <= comparisonValueMatches[2])));
  719. } else {
  720. return (valMatches[1] > comparisonValueMatches[1]) || // newer year
  721. // same year, newer month
  722. ((valMatches[1] === comparisonValueMatches[1]) && (valMatches[2] >= comparisonValueMatches[2]));
  723. }
  724. break;
  725. case "number":
  726. case "range":
  727. if (isMaxValidation) {
  728. return (!isNaN(val) && parseFloat(val) <= parseFloat(comparisonValue));
  729. } else {
  730. return (!isNaN(val) && parseFloat(val) >= parseFloat(comparisonValue));
  731. }
  732. break;
  733. default:
  734. if (isMaxValidation) {
  735. return val <= comparisonValue;
  736. } else {
  737. return val >= comparisonValue;
  738. }
  739. }
  740. };
  741. }
  742. kv.rules['min'] = {
  743. validator: minMaxValidatorFactory("min"),
  744. message: 'Please enter a value greater than or equal to {0}.'
  745. };
  746. kv.rules['max'] = {
  747. validator: minMaxValidatorFactory("max"),
  748. message: 'Please enter a value less than or equal to {0}.'
  749. };
  750. kv.rules['minLength'] = {
  751. validator: function (val, minLength) {
  752. if(kv.utils.isEmptyVal(val)) { return true; }
  753. var normalizedVal = kv.utils.isNumber(val) ? ('' + val) : val;
  754. return normalizedVal.length >= minLength;
  755. },
  756. message: 'Please enter at least {0} characters.'
  757. };
  758. kv.rules['maxLength'] = {
  759. validator: function (val, maxLength) {
  760. if(kv.utils.isEmptyVal(val)) { return true; }
  761. var normalizedVal = kv.utils.isNumber(val) ? ('' + val) : val;
  762. return normalizedVal.length <= maxLength;
  763. },
  764. message: 'Please enter no more than {0} characters.'
  765. };
  766. kv.rules['pattern'] = {
  767. validator: function (val, regex) {
  768. return kv.utils.isEmptyVal(val) || val.toString().match(regex) !== null;
  769. },
  770. message: 'Please check this value.'
  771. };
  772. kv.rules['step'] = {
  773. validator: function (val, step) {
  774. // in order to handle steps of .1 & .01 etc.. Modulus won't work
  775. // if the value is a decimal, so we have to correct for that
  776. if (kv.utils.isEmptyVal(val) || step === 'any') { return true; }
  777. var dif = (val * 100) % (step * 100);
  778. return Math.abs(dif) < 0.00001 || Math.abs(1 - dif) < 0.00001;
  779. },
  780. message: 'The value must increment by {0}.'
  781. };
  782. kv.rules['email'] = {
  783. validator: function (val, validate) {
  784. if (!validate) { return true; }
  785. //I think an empty email address is also a valid entry
  786. //if one want's to enforce entry it should be done with 'required: true'
  787. return kv.utils.isEmptyVal(val) || (
  788. // jquery validate regex - thanks Scott Gonzalez
  789. validate && /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(val)
  790. );
  791. },
  792. message: 'Please enter a proper email address.'
  793. };
  794. kv.rules['date'] = {
  795. validator: function (value, validate) {
  796. if (!validate) { return true; }
  797. return kv.utils.isEmptyVal(value) || (validate && !/Invalid|NaN/.test(new Date(value)));
  798. },
  799. message: 'Please enter a proper date.'
  800. };
  801. kv.rules['dateISO'] = {
  802. validator: function (value, validate) {
  803. if (!validate) { return true; }
  804. return kv.utils.isEmptyVal(value) || (validate && /^\d{4}[-/](?:0?[1-9]|1[012])[-/](?:0?[1-9]|[12][0-9]|3[01])$/.test(value));
  805. },
  806. message: 'Please enter a proper date.'
  807. };
  808. kv.rules['number'] = {
  809. validator: function (value, validate) {
  810. if (!validate) { return true; }
  811. return kv.utils.isEmptyVal(value) || (validate && /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value));
  812. },
  813. message: 'Please enter a number.'
  814. };
  815. kv.rules['digit'] = {
  816. validator: function (value, validate) {
  817. if (!validate) { return true; }
  818. return kv.utils.isEmptyVal(value) || (validate && /^\d+$/.test(value));
  819. },
  820. message: 'Please enter a digit.'
  821. };
  822. kv.rules['phoneUS'] = {
  823. validator: function (phoneNumber, validate) {
  824. if (!validate) { return true; }
  825. if (kv.utils.isEmptyVal(phoneNumber)) { return true; } // makes it optional, use 'required' rule if it should be required
  826. if (typeof (phoneNumber) !== 'string') { return false; }
  827. phoneNumber = phoneNumber.replace(/\s+/g, "");
  828. return validate && phoneNumber.length > 9 && phoneNumber.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  829. },
  830. message: 'Please specify a valid phone number.'
  831. };
  832. kv.rules['equal'] = {
  833. validator: function (val, params) {
  834. var otherValue = params;
  835. return val === kv.utils.getValue(otherValue);
  836. },
  837. message: 'Values must equal.'
  838. };
  839. kv.rules['notEqual'] = {
  840. validator: function (val, params) {
  841. var otherValue = params;
  842. return val !== kv.utils.getValue(otherValue);
  843. },
  844. message: 'Please choose another value.'
  845. };
  846. //unique in collection
  847. // options are:
  848. // collection: array or function returning (observable) array
  849. // in which the value has to be unique
  850. // valueAccessor: function that returns value from an object stored in collection
  851. // if it is null the value is compared directly
  852. // external: set to true when object you are validating is automatically updating collection
  853. kv.rules['unique'] = {
  854. validator: function (val, options) {
  855. var c = kv.utils.getValue(options.collection),
  856. external = kv.utils.getValue(options.externalValue),
  857. counter = 0;
  858. if (!val || !c) { return true; }
  859. koUtils.arrayFilter(c, function (item) {
  860. if (val === (options.valueAccessor ? options.valueAccessor(item) : item)) { counter++; }
  861. });
  862. // if value is external even 1 same value in collection means the value is not unique
  863. return counter < (!!external ? 1 : 2);
  864. },
  865. message: 'Please make sure the value is unique.'
  866. };
  867. //now register all of these!
  868. (function () {
  869. kv.registerExtenders();
  870. }());
  871. ;// The core binding handler
  872. // this allows us to setup any value binding that internally always
  873. // performs the same functionality
  874. ko.bindingHandlers['validationCore'] = (function () {
  875. return {
  876. init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  877. var config = kv.utils.getConfigOptions(element);
  878. var observable = valueAccessor();
  879. // parse html5 input validation attributes, optional feature
  880. if (config.parseInputAttributes) {
  881. kv.utils.async(function () { kv.parseInputValidationAttributes(element, valueAccessor); });
  882. }
  883. // if requested insert message element and apply bindings
  884. if (config.insertMessages && kv.utils.isValidatable(observable)) {
  885. // insert the <span></span>
  886. var validationMessageElement = kv.insertValidationMessage(element);
  887. // if we're told to use a template, make sure that gets rendered
  888. if (config.messageTemplate) {
  889. ko.renderTemplate(config.messageTemplate, { field: observable }, null, validationMessageElement, 'replaceNode');
  890. } else {
  891. ko.applyBindingsToNode(validationMessageElement, { validationMessage: observable });
  892. }
  893. }
  894. // write the html5 attributes if indicated by the config
  895. if (config.writeInputAttributes && kv.utils.isValidatable(observable)) {
  896. kv.writeInputValidationAttributes(element, valueAccessor);
  897. }
  898. // if requested, add binding to decorate element
  899. if (config.decorateInputElement && kv.utils.isValidatable(observable)) {
  900. ko.applyBindingsToNode(element, { validationElement: observable });
  901. }
  902. }
  903. };
  904. }());
  905. // override for KO's default 'value', 'checked', 'textInput' and selectedOptions bindings
  906. kv.makeBindingHandlerValidatable("value");
  907. kv.makeBindingHandlerValidatable("checked");
  908. if (ko.bindingHandlers.textInput) {
  909. kv.makeBindingHandlerValidatable("textInput");
  910. }
  911. kv.makeBindingHandlerValidatable("selectedOptions");
  912. ko.bindingHandlers['validationMessage'] = { // individual error message, if modified or post binding
  913. update: function (element, valueAccessor) {
  914. var obsv = valueAccessor(),
  915. config = kv.utils.getConfigOptions(element),
  916. val = unwrap(obsv),
  917. msg = null,
  918. isModified = false,
  919. isValid = false;
  920. if (obsv === null || typeof obsv === 'undefined') {
  921. throw new Error('Cannot bind validationMessage to undefined value. data-bind expression: ' +
  922. element.getAttribute('data-bind'));
  923. }
  924. isModified = obsv.isModified && obsv.isModified();
  925. isValid = obsv.isValid && obsv.isValid();
  926. var error = null;
  927. if (!config.messagesOnModified || isModified) {
  928. error = isValid ? null : obsv.error;
  929. }
  930. var isVisible = !config.messagesOnModified || isModified ? !isValid : false;
  931. var isCurrentlyVisible = element.style.display !== "none";
  932. if (config.allowHtmlMessages) {
  933. koUtils.setHtml(element, error);
  934. } else {
  935. ko.bindingHandlers.text.update(element, function () { return error; });
  936. }
  937. if (isCurrentlyVisible && !isVisible) {
  938. element.style.display = 'none';
  939. } else if (!isCurrentlyVisible && isVisible) {
  940. element.style.display = '';
  941. }
  942. }
  943. };
  944. ko.bindingHandlers['validationElement'] = {
  945. update: function (element, valueAccessor, allBindingsAccessor) {
  946. var obsv = valueAccessor(),
  947. config = kv.utils.getConfigOptions(element),
  948. val = unwrap(obsv),
  949. msg = null,
  950. isModified = false,
  951. isValid = false;
  952. if (obsv === null || typeof obsv === 'undefined') {
  953. throw new Error('Cannot bind validationElement to undefined value. data-bind expression: ' +
  954. element.getAttribute('data-bind'));
  955. }
  956. isModified = obsv.isModified && obsv.isModified();
  957. isValid = obsv.isValid && obsv.isValid();
  958. // create an evaluator function that will return something like:
  959. // css: { validationElement: true }
  960. var cssSettingsAccessor = function () {
  961. var css = {};
  962. var shouldShow = ((!config.decorateElementOnModified || isModified) ? !isValid : false);
  963. // css: { validationElement: false }
  964. css[config.errorElementClass] = shouldShow;
  965. return css;
  966. };
  967. //add or remove class on the element;
  968. ko.bindingHandlers.css.update(element, cssSettingsAccessor, allBindingsAccessor);
  969. if (!config.errorsAsTitle) { return; }
  970. ko.bindingHandlers.attr.update(element, function () {
  971. var
  972. hasModification = !config.errorsAsTitleOnModified || isModified,
  973. title = kv.utils.getOriginalElementTitle(element);
  974. if (hasModification && !isValid) {
  975. return { title: obsv.error, 'data-orig-title': title };
  976. } else if (!hasModification || isValid) {
  977. return { title: title, 'data-orig-title': null };
  978. }
  979. });
  980. }
  981. };
  982. // ValidationOptions:
  983. // This binding handler allows you to override the initial config by setting any of the options for a specific element or context of elements
  984. //
  985. // Example:
  986. // <div data-bind="validationOptions: { insertMessages: true, messageTemplate: 'customTemplate', errorMessageClass: 'mySpecialClass'}">
  987. // <input type="text" data-bind="value: someValue"/>
  988. // <input type="text" data-bind="value: someValue2"/>
  989. // </div>
  990. ko.bindingHandlers['validationOptions'] = (function () {
  991. return {
  992. init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  993. var options = unwrap(valueAccessor());
  994. if (options) {
  995. var newConfig = extend({}, kv.configuration);
  996. extend(newConfig, options);
  997. //store the validation options on the node so we can retrieve it later
  998. kv.utils.setDomData(element, newConfig);
  999. }
  1000. }
  1001. };
  1002. }());
  1003. ;// Validation Extender:
  1004. // This is for creating custom validation logic on the fly
  1005. // Example:
  1006. // var test = ko.observable('something').extend{(
  1007. // validation: {
  1008. // validator: function(val, someOtherVal){
  1009. // return true;
  1010. // },
  1011. // message: "Something must be really wrong!',
  1012. // params: true
  1013. // }
  1014. // )};
  1015. ko.extenders['validation'] = function (observable, rules) { // allow single rule or array
  1016. forEach(kv.utils.isArray(rules) ? rules : [rules], function (rule) {
  1017. // the 'rule' being passed in here has no name to identify a core Rule,
  1018. // so we add it as an anonymous rule
  1019. // If the developer is wanting to use a core Rule, but use a different message see the 'addExtender' logic for examples
  1020. kv.addAnonymousRule(observable, rule);
  1021. });
  1022. return observable;
  1023. };
  1024. //This is the extender that makes a Knockout Observable also 'Validatable'
  1025. //examples include:
  1026. // 1. var test = ko.observable('something').extend({validatable: true});
  1027. // this will ensure that the Observable object is setup properly to respond to rules
  1028. //
  1029. // 2. test.extend({validatable: false});
  1030. // this will remove the validation properties from the Observable object should you need to do that.
  1031. ko.extenders['validatable'] = function (observable, options) {
  1032. if (!kv.utils.isObject(options)) {
  1033. options = { enable: options };
  1034. }
  1035. if (!('enable' in options)) {
  1036. options.enable = true;
  1037. }
  1038. if (options.enable && !kv.utils.isValidatable(observable)) {
  1039. var config = kv.configuration.validate || {};
  1040. var validationOptions = {
  1041. throttleEvaluation : options.throttle || config.throttle
  1042. };
  1043. observable.error = ko.observable(null); // holds the error message, we only need one since we stop processing validators when one is invalid
  1044. // observable.rules:
  1045. // ObservableArray of Rule Contexts, where a Rule Context is simply the name of a rule and the params to supply to it
  1046. //
  1047. // Rule Context = { rule: '<rule name>', params: '<passed in params>', message: '<Override of default Message>' }
  1048. observable.rules = ko.observableArray(); //holds the rule Contexts to use as part of validation
  1049. //in case async validation is occurring
  1050. observable.isValidating = ko.observable(false);
  1051. //the true holder of whether the observable is valid or not
  1052. observable.__valid__ = ko.observable(true);
  1053. observable.isModified = ko.observable(false);
  1054. // a semi-protected observable
  1055. observable.isValid = ko.computed(observable.__valid__);
  1056. //manually set error state
  1057. observable.setError = function (error) {
  1058. var previousError = observable.error.peek();
  1059. var previousIsValid = observable.__valid__.peek();
  1060. observable.error(error);
  1061. observable.__valid__(false);
  1062. if (previousError !== error && !previousIsValid) {
  1063. // if the observable was not valid before then isValid will not mutate,
  1064. // hence causing any grouping to not display the latest error.
  1065. observable.isValid.notifySubscribers();
  1066. }
  1067. };
  1068. //manually clear error state
  1069. observable.clearError = function () {
  1070. observable.error(null);
  1071. observable.__valid__(true);
  1072. return observable;
  1073. };
  1074. //subscribe to changes in the observable
  1075. var h_change = observable.subscribe(function () {
  1076. observable.isModified(true);
  1077. });
  1078. // we use a computed here to ensure that anytime a dependency changes, the
  1079. // validation logic evaluates
  1080. var h_obsValidationTrigger = ko.computed(extend({
  1081. read: function () {
  1082. var obs = observable(),
  1083. ruleContexts = observable.rules();
  1084. kv.validateObservable(observable);
  1085. return true;
  1086. }
  1087. }, validationOptions));
  1088. extend(h_obsValidationTrigger, validationOptions);
  1089. observable._disposeValidation = function () {
  1090. //first dispose of the subscriptions
  1091. observable.isValid.dispose();
  1092. observable.rules.removeAll();
  1093. h_change.dispose();
  1094. h_obsValidationTrigger.dispose();
  1095. delete observable['rules'];
  1096. delete observable['error'];
  1097. delete observable['isValid'];
  1098. delete observable['isValidating'];
  1099. delete observable['__valid__'];
  1100. delete observable['isModified'];
  1101. delete observable['setError'];
  1102. delete observable['clearError'];
  1103. delete observable['_disposeValidation'];
  1104. };
  1105. } else if (options.enable === false && observable._disposeValidation) {
  1106. observable._disposeValidation();
  1107. }
  1108. return observable;
  1109. };
  1110. function validateSync(observable, rule, ctx) {
  1111. //Execute the validator and see if its valid
  1112. if (!rule.validator(observable(), (ctx.params === undefined ? true : unwrap(ctx.params)))) { // default param is true, eg. required = true
  1113. //not valid, so format the error message and stick it in the 'error' variable
  1114. observable.setError(kv.formatMessage(
  1115. ctx.message || rule.message,
  1116. unwrap(ctx.params),
  1117. observable));
  1118. return false;
  1119. } else {
  1120. return true;
  1121. }
  1122. }
  1123. function validateAsync(observable, rule, ctx) {
  1124. observable.isValidating(true);
  1125. var callBack = function (valObj) {
  1126. var isValid = false,
  1127. msg = '';
  1128. if (!observable.__valid__()) {
  1129. // since we're returning early, make sure we turn this off
  1130. observable.isValidating(false);
  1131. return; //if its already NOT valid, don't add to that
  1132. }
  1133. //we were handed back a complex object
  1134. if (valObj['message']) {
  1135. isValid = valObj.isValid;
  1136. msg = valObj.message;
  1137. } else {
  1138. isValid = valObj;
  1139. }
  1140. if (!isValid) {
  1141. //not valid, so format the error message and stick it in the 'error' variable
  1142. observable.error(kv.formatMessage(
  1143. msg || ctx.message || rule.message,
  1144. unwrap(ctx.params),
  1145. observable));
  1146. observable.__valid__(isValid);
  1147. }
  1148. // tell it that we're done
  1149. observable.isValidating(false);
  1150. };
  1151. //fire the validator and hand it the callback
  1152. rule.validator(observable(), unwrap(ctx.params || true), callBack);
  1153. }
  1154. kv.validateObservable = function (observable) {
  1155. var i = 0,
  1156. rule, // the rule validator to execute
  1157. ctx, // the current Rule Context for the loop
  1158. ruleContexts = observable.rules(), //cache for iterator
  1159. len = ruleContexts.length; //cache for iterator
  1160. for (; i < len; i++) {
  1161. //get the Rule Context info to give to the core Rule
  1162. ctx = ruleContexts[i];
  1163. // checks an 'onlyIf' condition
  1164. if (ctx.condition && !ctx.condition()) {
  1165. continue;
  1166. }
  1167. //get the core Rule to use for validation
  1168. rule = ctx.rule ? kv.rules[ctx.rule] : ctx;
  1169. if (rule['async'] || ctx['async']) {
  1170. //run async validation
  1171. validateAsync(observable, rule, ctx);
  1172. } else {
  1173. //run normal sync validation
  1174. if (!validateSync(observable, rule, ctx)) {
  1175. return false; //break out of the loop
  1176. }
  1177. }
  1178. }
  1179. //finally if we got this far, make the observable valid again!
  1180. observable.clearError();
  1181. return true;
  1182. };
  1183. ;
  1184. var _locales = {};
  1185. var _currentLocale;
  1186. kv.defineLocale = function(name, values) {
  1187. if (name && values) {
  1188. _locales[name.toLowerCase()] = values;
  1189. return values;
  1190. }
  1191. return null;
  1192. };
  1193. kv.locale = function(name) {
  1194. if (name) {
  1195. name = name.toLowerCase();
  1196. if (_locales.hasOwnProperty(name)) {
  1197. kv.localize(_locales[name]);
  1198. _currentLocale = name;
  1199. }
  1200. else {
  1201. throw new Error('Localization ' + name + ' has not been loaded.');
  1202. }
  1203. }
  1204. return _currentLocale;
  1205. };
  1206. //quick function to override rule messages
  1207. kv.localize = function (msgTranslations) {
  1208. var rules = kv.rules;
  1209. //loop the properties in the object and assign the msg to the rule
  1210. for (var ruleName in msgTranslations) {
  1211. if (rules.hasOwnProperty(ruleName)) {
  1212. rules[ruleName].message = msgTranslations[ruleName];
  1213. }
  1214. }
  1215. };
  1216. // Populate default locale (this will make en-US.js somewhat redundant)
  1217. (function() {
  1218. var localeData = {};
  1219. var rules = kv.rules;
  1220. for (var ruleName in rules) {
  1221. if (rules.hasOwnProperty(ruleName)) {
  1222. localeData[ruleName] = rules[ruleName].message;
  1223. }
  1224. }
  1225. kv.defineLocale('en-us', localeData);
  1226. })();
  1227. // No need to invoke locale because the messages are already defined along with the rules for en-US
  1228. _currentLocale = 'en-us';
  1229. ;/**
  1230. * Possible invocations:
  1231. * applyBindingsWithValidation(viewModel)
  1232. * applyBindingsWithValidation(viewModel, options)
  1233. * applyBindingsWithValidation(viewModel, rootNode)
  1234. * applyBindingsWithValidation(viewModel, rootNode, options)
  1235. */
  1236. ko.applyBindingsWithValidation = function (viewModel, rootNode, options) {
  1237. var node = document.body,
  1238. config;
  1239. if (rootNode && rootNode.nodeType) {
  1240. node = rootNode;
  1241. config = options;
  1242. }
  1243. else {
  1244. config = rootNode;
  1245. }
  1246. kv.init();
  1247. if (config) {
  1248. config = extend(extend({}, kv.configuration), config);
  1249. kv.utils.setDomData(node, config);
  1250. }
  1251. ko.applyBindings(viewModel, node);
  1252. };
  1253. //override the original applyBindings so that we can ensure all new rules and what not are correctly registered
  1254. var origApplyBindings = ko.applyBindings;
  1255. ko.applyBindings = function (viewModel, rootNode) {
  1256. kv.init();
  1257. origApplyBindings(viewModel, rootNode);
  1258. };
  1259. ko.validatedObservable = function (initialValue, options) {
  1260. if (!options && !kv.utils.isObject(initialValue)) {
  1261. return ko.observable(initialValue).extend({ validatable: true });
  1262. }
  1263. var obsv = ko.observable(initialValue);
  1264. obsv.errors = kv.group(kv.utils.isObject(initialValue) ? initialValue : {}, options);
  1265. obsv.isValid = ko.observable(obsv.errors().length === 0);
  1266. if (ko.isObservable(obsv.errors)) {
  1267. obsv.errors.subscribe(function(errors) {
  1268. obsv.isValid(errors.length === 0);
  1269. });
  1270. }
  1271. else {
  1272. ko.computed(obsv.errors).subscribe(function (errors) {
  1273. obsv.isValid(errors.length === 0);
  1274. });
  1275. }
  1276. obsv.subscribe(function(newValue) {
  1277. if (!kv.utils.isObject(newValue)) {
  1278. /*
  1279. * The validation group works on objects.
  1280. * Since the new value is a primitive (scalar, null or undefined) we need
  1281. * to create an empty object to pass along.
  1282. */
  1283. newValue = {};
  1284. }
  1285. // Force the group to refresh
  1286. obsv.errors._updateState(newValue);
  1287. obsv.isValid(obsv.errors().length === 0);
  1288. });
  1289. return obsv;
  1290. };
  1291. ;}));