This commit is contained in:
2019-03-22 19:54:11 +00:00
parent 80034af489
commit d41dd49c3b
2 changed files with 99 additions and 51 deletions

View File

@@ -1,4 +1,4 @@
/* eslint-disable */ /* Xeslint-disable */
/////////////////////////////// ///////////////////////////////
// GZVALIDATE // GZVALIDATE
// //
@@ -9,6 +9,7 @@
import dayjs from "dayjs"; import dayjs from "dayjs";
import locale from "./locale"; import locale from "./locale";
import _ from "../libs/lodash.min.js"; import _ from "../libs/lodash.min.js";
import errorHandler from "./errorhandler";
function isEmpty(o) { function isEmpty(o) {
if (typeof o == "number" && o == 0) { if (typeof o == "number" && o == 0) {
@@ -20,10 +21,16 @@ function isEmpty(o) {
//////////////////////////////////// ////////////////////////////////////
// Get control from ref // Get control from ref
// //
function getControl(ref) { function getControl(v, ref) {
var ctrl = v.$refs[ref]; var ctrl = v.$refs[ref];
if (isEmpty(ctrl)) { if (errorHandler.developmentModeShowErrorsImmediately) {
return null; if (isEmpty(ctrl)) {
throw "GZValidate:getControl - the control has no label " + ctrl;
}
} else {
if (isEmpty(ctrl)) {
return null;
}
} }
} }
@@ -31,6 +38,11 @@ function getControl(ref) {
// Get value from control // Get value from control
// //
function getControlValue(ctrl) { function getControlValue(ctrl) {
/*
ctrl.$refs[ref].value 0
ctrl.$refs[ref].label "Count"
ctrl.$refs[ref].initialValue
*/
var value = ctrl.value; var value = ctrl.value;
return value; return value;
} }
@@ -38,35 +50,21 @@ function getControlValue(ctrl) {
//////////////////////////////////// ////////////////////////////////////
// Get field name from control // Get field name from control
// //
function getControlFieldName(ctrl) { function getControlLabel(ctrl) {
if (errorHandler.developmentModeShowErrorsImmediately) {
if (!ctrl.label) {
throw "GZValidate:getControlLabel - the control has no label " + ctrl;
}
}
return ctrl.label; return ctrl.label;
} }
export default { export default {
/*
ctrl.$refs[ref].value
0
ctrl.$refs[ref].label
"Count"
ctrl.$refs[ref].initialValue
*/
/////////////////////////////// ///////////////////////////////
// REQUIRED // REQUIRED
// Required(ltkey, value) { //
// if (!_.isEmpty(value)) {
// return false;
// }
// //get the localized rule text
// // "ErrorRequiredFieldEmpty": "{0} is a required field. Please enter a value for {0}",
// var err = locale.get("ErrorRequiredFieldEmpty");
// var fieldName = locale.get(ltkey);
// err = _.replace(err, "{0}", fieldName);
// //lodash replace only replaces first instance so need to do it twice
// err = _.replace(err, "{0}", fieldName);
// return err;
// },
Required(v, ref) { Required(v, ref) {
var ctrl = getControl(ref); var ctrl = getControl(v, ref);
if (!ctrl) { if (!ctrl) {
return false; return false;
} }
@@ -75,10 +73,10 @@ ctrl.$refs[ref].initialValue
if (!isEmpty(value)) { if (!isEmpty(value)) {
return false; return false;
} }
//get the localized rule text
// "ErrorRequiredFieldEmpty": "{0} is a required field. Please enter a value for {0}", // "ErrorRequiredFieldEmpty": "{0} is a required field. Please enter a value for {0}",
var err = locale.get("ErrorRequiredFieldEmpty"); var err = locale.get("ErrorRequiredFieldEmpty");
var fieldName = ctrl.label; var fieldName = getControlLabel(ctrl);
err = _.replace(err, "{0}", fieldName); err = _.replace(err, "{0}", fieldName);
//lodash replace only replaces first instance so need to do it twice //lodash replace only replaces first instance so need to do it twice
err = _.replace(err, "{0}", fieldName); err = _.replace(err, "{0}", fieldName);
@@ -86,8 +84,18 @@ ctrl.$refs[ref].initialValue
}, },
/////////////////////////////// ///////////////////////////////
// MAXLENGTH // MAXLENGTH
//MaxLength(ltkey, value, max) { //
MaxLength(v, ref) { MaxLength(v, ref, max) {
var ctrl = getControl(v, ref);
if (!ctrl) {
return false;
}
var value = getControlValue(ctrl);
if (!isEmpty(value)) {
return false;
}
if (_.isEmpty(value)) { if (_.isEmpty(value)) {
return false; return false;
} }
@@ -96,7 +104,7 @@ ctrl.$refs[ref].initialValue
//get the localized rule text //get the localized rule text
// "ErrorFieldLengthExceeded": "{0} can not exceed {1} characters.", // "ErrorFieldLengthExceeded": "{0} can not exceed {1} characters.",
var err = locale.get("ErrorFieldLengthExceeded"); var err = locale.get("ErrorFieldLengthExceeded");
var fieldName = locale.get(ltkey); var fieldName = getControlLabel(ctrl);
err = _.replace(err, "{0}", fieldName); err = _.replace(err, "{0}", fieldName);
err = _.replace(err, "{1}", max); err = _.replace(err, "{1}", max);
return err; return err;
@@ -106,40 +114,80 @@ ctrl.$refs[ref].initialValue
}, },
/////////////////////////////// ///////////////////////////////
// MAX 255 // MAX 255
Max255(ltkey, value) { //
return this.MaxLength(ltkey, value, 255); Max255(v, ref) {
return this.MaxLength(v, ref, 255);
}, },
// ///////////////////////////////
// // AFTER
// After(startDate, endDate) {
// if (_.isEmpty(startDate)) {
// return false;
// }
// if (_.isEmpty(endDate)) {
// return false;
// }
// startDate = dayjs(startDate);
// endDate = dayjs(endDate);
// // if either is not valid.
// if (!startDate || !endDate) {
// return false;
// }
// if (startDate.isAfter(endDate)) {
// // "ErrorStartDateAfterEndDate": "Start date must be earlier than stop / end date",
// var err = locale.get("ErrorStartDateAfterEndDate");
// return err;
// } else {
// return false;
// }
// },
/////////////////////////////// ///////////////////////////////
// AFTER // AFTER
After(startDate, endDate) { After(v, refStart, refEnd) {
if (_.isEmpty(startDate)) { var ctrlStart = getControl(v, refStart);
return false; if (!ctrlStart) {
}
if (_.isEmpty(endDate)) {
return false; return false;
} }
startDate = dayjs(startDate); var ctrlEnd = getControl(v, refEnd);
endDate = dayjs(endDate); if (!ctrlEnd) {
return false;
}
var valueStart = getControlValue(ctrlStart);
if (!isEmpty(valueStart)) {
return false;
}
var valueEnd = getControlValue(ctrlEnd);
if (!isEmpty(valueEnd)) {
return false;
}
if (_.isEmpty(valueStart)) {
return false;
}
if (_.isEmpty(valueEnd)) {
return false;
}
valueStart = dayjs(valueStart);
valueEnd = dayjs(valueEnd);
// if either is not valid. // if either is not valid.
if (!startDate || !endDate) { if (!valueStart || !valueEnd) {
return false; return false;
} }
if (startDate.isAfter(endDate)) { if (valueStart.isAfter(valueEnd)) {
// "ErrorStartDateAfterEndDate": "Start date must be earlier than stop / end date", // "ErrorStartDateAfterEndDate": "Start date must be earlier than stop / end date",
var err = locale.get("ErrorStartDateAfterEndDate"); var err = locale.get("ErrorStartDateAfterEndDate");
return err; return err;
} else { } else {
return false; return false;
} }
},
///////////////////////////////
// TEST
Test(ctrl, ref) {
var v = ctrl;
return false;
// return this.MaxLength(ltkey, value, 255);
} }
}; };

View File

@@ -8,7 +8,7 @@
v-model="obj.name" v-model="obj.name"
:counter="255" :counter="255"
:label="this.$gzlocale.get('WidgetName')" :label="this.$gzlocale.get('WidgetName')"
:rules="[this.$gzv.Max255('WidgetName',obj.name)]" :rules="[this.$gzv.Max255(this,'WidgetName')]"
ref="name" ref="name"
></v-text-field> ></v-text-field>
</v-flex> </v-flex>
@@ -17,7 +17,7 @@
v-model="obj.serial" v-model="obj.serial"
:counter="10" :counter="10"
:label="this.$gzlocale.get('WidgetSerial')" :label="this.$gzlocale.get('WidgetSerial')"
:rules="[this.$gzv.MaxLength('WidgetSerial',obj.serial,10)]" :rules="[this.$gzv.MaxLength(this,'WidgetSerial',10)]"
ref="serial" ref="serial"
></v-text-field> ></v-text-field>
</v-flex> </v-flex>