Skip to content

Commit

Permalink
fix js валидатора
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhelskiy committed Apr 5, 2015
1 parent 08c6dc8 commit 3d66e6d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions frontend/components/field/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
"upload-area": "field.upload-area.tpl"
},
"scripts": {
"validation": "js/validation.js",

"vendor-chosen": "vendor/chosen/chosen.jquery.min.js",
"vendor-chosen-order": "vendor/chosen-order/chosen.order.jquery.js",
"vendor-parsley": "vendor/parsley/dist/parsley.remote.js",
"vendor-parsley-ru": "vendor/parsley/i18n/ru.js",
"jquery-ui-timepicker-addon": "vendor/timepicker/jquery-ui-timepicker-addon.js",

"validation": "js/validation.js",

"date": "js/field.date.js",
"datetime": "js/field.datetime.js",
"time": "js/field.time.js",
Expand Down
3 changes: 3 additions & 0 deletions frontend/components/field/field.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
{/if}

{if $rule@key === 'remote'}
data-parsley-remote-validator="{$_rules['remote-validator']|default:'fields'}"
data-parsley-trigger="focusout"

{* Default remote options *}
{$json = [ 'type' => 'post', 'data' => [ 'security_ls_key' => $LIVESTREET_SECURITY_KEY ] ]}

Expand Down
39 changes: 37 additions & 2 deletions frontend/components/field/js/validation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
/**
* Кастомные валидаторы
*/
window.ParsleyExtend = window.ParsleyExtend || {};
window.ParsleyExtend = $.extend(window.ParsleyExtend, {
asyncValidators: $.extend({
fields: {
fn: function (xhr) {
this._remoteCache = {};

jQuery(document).ready(function($) {
xhr.done(function (response) {
if ('undefined' !== typeof response.aErrors) {
var name = this.$element.attr('name') || this.$element.attr('id');
var msg = response.aErrors[name].join("<br>");
window.ParsleyUI.updateError(this, 'remote', msg);
return false
}
}.bind(this));

return 'undefined' === typeof xhr.responseJSON.aErrors;
},
prepare: function () {
var params = {
data: {
fields: {
0: {
field: this.$element.attr('name') || this.$element.attr('id'),
value: this.getValue()
}
}
}
};
this.options.remoteOptions = $.extend(true, this.options.remoteOptions || {}, params);
},
url: false
}
}, window.ParsleyExtend.asyncValidators)
});

jQuery(document).ready(function ($) {
ParsleyValidator
.addValidator('rangetags', function (value, requirement) {
var tag_count = value.replace(/ /g, "").match(/[^\s,]+(,|)/gi);
return tag_count && tag_count.length >= requirement[0] && tag_count.length <= requirement[1];
}, 50)
.addMessage(LANGUAGE, 'rangetags', ls.lang.get('validate.tags.count', { 'field': '', 'min': '%s', 'max': '%s' }));
.addMessage(LANGUAGE, 'rangetags', ls.lang.get('validate.tags.count', {'field': '', 'min': '%s', 'max': '%s'}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ window.ParsleyExtend = $.extend(window.ParsleyExtend, {
// Merge options passed in from the function with the ones in the attribute
this.options.remoteOptions = $.extend(true, this.options.remoteOptions || {} , this.asyncValidators[validator].options);

/* @livestreet fix begin */
if ('function' === typeof this.asyncValidators[validator].prepare) {
this.asyncValidators[validator].prepare.call(this);
}
/* @livestreet fix end */

// All `$.ajax(options)` could be overridden or extended directly from DOM in `data-parsley-remote-options`
ajaxOptions = $.extend(true, {}, {
url: this.asyncValidators[validator].url || this.options.remote,
Expand Down

0 comments on commit 3d66e6d

Please sign in to comment.