Skip to content

Commit

Permalink
fix: use static accessors for the locale changes prior to installing …
Browse files Browse the repository at this point in the history
…the plugin closes #1664, closes #1662, closes #1647, closes #1646
  • Loading branch information
logaretm committed Oct 22, 2018
1 parent 73f30c2 commit bf9db63
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/core/validator.js
Expand Up @@ -2,7 +2,7 @@ import ErrorBag from './errorBag';
import FieldBag from './fieldBag';
import RuleContainer from './ruleContainer';
import Field from './field';
import { pluginInstance as VeeValidate } from '../plugin';
import VeeValidate, { pluginInstance as Vee } from '../plugin';
import {
isObject,
getPath,
Expand Down Expand Up @@ -43,6 +43,14 @@ export default class Validator {
return RuleContainer.rules;
}

get dictionary () {
return VeeValidate.i18nDriver;
}

static get dictionary () {
return VeeValidate.i18nDriver;
}

get flags () {
return this.fields.items.reduce((acc, field) => {
if (field.scope) {
Expand All @@ -59,17 +67,6 @@ export default class Validator {
}, {});
}

/**
* Getter for the dictionary.
*/
get dictionary (): IDictionary {
return VeeValidate.i18nDriver;
}

static get dictionary () {
return VeeValidate.i18nDriver;
}

/**
* Getter for the current locale.
*/
Expand All @@ -85,17 +82,17 @@ export default class Validator {
}

static get locale () {
return this.dictionary.locale;
return VeeValidate.i18nDriver.locale;
}

/**
* Setter for the validator locale.
*/
static set locale (value) {
const hasChanged = value !== Validator.dictionary.locale;
Validator.dictionary.locale = value;
if (hasChanged && VeeValidate._vm) {
VeeValidate._vm.$emit('localeChanged');
const hasChanged = value !== VeeValidate.i18nDriver.locale;
VeeValidate.i18nDriver.locale = value;
if (hasChanged && Vee._vm) {
Vee._vm.$emit('localeChanged');
}
}

Expand Down Expand Up @@ -144,15 +141,15 @@ export default class Validator {
*/
static localize (lang: string | MapObject, dictionary?: MapObject) {
if (isObject(lang)) {
Validator.dictionary.merge(lang);
VeeValidate.i18nDriver.merge(lang);
return;
}

// merge the dictionary.
if (dictionary) {
const locale = lang || dictionary.name;
dictionary = assign({}, dictionary);
Validator.dictionary.merge({
VeeValidate.i18nDriver.merge({
[locale]: dictionary
});
}
Expand Down Expand Up @@ -218,8 +215,8 @@ export default class Validator {

reset (matcher) {
// two ticks
return VeeValidate._vm.$nextTick().then(() => {
return VeeValidate._vm.$nextTick();
return Vee._vm.$nextTick().then(() => {
return Vee._vm.$nextTick();
}).then(() => {
this.fields.filter(matcher).forEach(field => {
field.waitFor(null);
Expand Down Expand Up @@ -392,7 +389,7 @@ export default class Validator {
* Perform cleanup.
*/
destroy () {
VeeValidate._vm.$off('localeChanged');
Vee._vm.$off('localeChanged');
}

/**
Expand All @@ -416,7 +413,7 @@ export default class Validator {
format = validations.date_format[0];
}

return format || this.dictionary.getDateFormat(this.locale);
return format || VeeValidate.i18nDriver.getDateFormat(this.locale);
}

/**
Expand All @@ -426,7 +423,7 @@ export default class Validator {
const name = this._getFieldDisplayName(field);
const params = this._getLocalizedParams(rule, targetName);

return this.dictionary.getFieldMessage(this.locale, field.name, rule.name, [name, params, data]);
return VeeValidate.i18nDriver.getFieldMessage(this.locale, field.name, rule.name, [name, params, data]);
}

/**
Expand Down Expand Up @@ -457,7 +454,7 @@ export default class Validator {
_getLocalizedParams (rule: MapObject, targetName?: string | null = null) {
let params = this._convertParamObjectToArray(rule.params, rule.name);
if (rule.options.hasTarget && params && params[0]) {
const localizedName = targetName || this.dictionary.getAttribute(this.locale, params[0], params[0]);
const localizedName = targetName || VeeValidate.i18nDriver.getAttribute(this.locale, params[0], params[0]);
return [localizedName].concat(params.slice(1));
}

Expand All @@ -468,7 +465,7 @@ export default class Validator {
* Resolves an appropriate display name, first checking 'data-as' or the registered 'prettyName'
*/
_getFieldDisplayName (field: Field) {
return field.alias || this.dictionary.getAttribute(this.locale, field.name, field.name);
return field.alias || VeeValidate.i18nDriver.getAttribute(this.locale, field.name, field.name);
}

/**
Expand Down Expand Up @@ -572,7 +569,7 @@ export default class Validator {
static _merge (name: string, { validator, options, paramNames }) {
const validate = isCallable(validator) ? validator : validator.validate;
if (validator.getMessage) {
Validator.dictionary.setMessage(Validator.locale, name, validator.getMessage);
VeeValidate.i18nDriver.setMessage(Validator.locale, name, validator.getMessage);
}

RuleContainer.add(name, {
Expand Down

0 comments on commit bf9db63

Please sign in to comment.