/*! * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2017 * @version 1.3.2 * * Client validation extension for the yii2-field-range extension * * Author: Kartik Visweswaran * For more JQuery plugins visit http://plugins.krajee.com * For more Yii related demos visit http://demos.krajee.com */ (function ($) { "use strict"; var isArray, setContextCss, KvFieldRange; isArray = function (a) { return Object.prototype.toString.call(a) === '[object Array]' || Object.prototype.toString.call(a) === '[object Object]'; }; setContextCss = function(id, css) { var $el = $('#' + id).closest('.' + css), altCss; if (!$el.length) { return; } altCss = css === 'has-success' ? 'has-error' : 'has-success'; $el.removeClass(altCss).addClass(css); }; KvFieldRange = function (element, options) { var self = this; self.$attrTo = $(element); self.$attrFrom = $("#" + options.attrFrom); self.$mainContainer = $("#" + options.container); self.$errorContainer = $("#" + options.errorContainer); self.$errorBlockFrom = self.$attrFrom.closest('.kv-container-from').find('.help-block'); self.$errorBlockTo = self.$attrTo.closest('.kv-container-to').find('.help-block'); self.$errorBlock = self.$errorContainer.find('.help-block'); self.$form = self.$attrFrom.closest('form'); self.errorToMsg = ""; self.init(); }; KvFieldRange.prototype = { constructor: KvFieldRange, init: function () { var self = this; self.$errorBlockFrom.hide(); self.$errorBlockTo.hide(); self.$form.on('reset.yiiActiveForm', function () { setTimeout(function () { self.reset(); }, 100); }); self.$form.on('afterValidate', function (event, messages) { var idFrom = self.$attrFrom.attr('id'), idTo = self.$attrTo.attr('id'); if (idFrom in messages) { self.validateAttribute(messages[idFrom], idFrom, idTo); } if (idTo in messages) { self.validateAttribute(messages[idTo], idFrom, idTo); } }); self.$form.on('afterValidateAttribute', function (event, attribute, messages) { var idFrom = self.$attrFrom.attr('id'), idTo = self.$attrTo.attr('id'); self.$errorBlock.html(''); self.$errorContainer.removeClass('has-success has-error'); if (attribute.id === idFrom || attribute.id === idTo) { self.validateAttribute(messages, idFrom, idTo); } }); }, validateAttribute: function (msg, idFrom, idTo) { var self = this, len = msg.length, errMsg = ''; if (isArray(msg) && len) { errMsg = len === 1 ? msg[0] : msg.join('