This commit is contained in:
2019-03-28 18:46:42 +00:00
parent 383ec182d6
commit 3d083967f5
2 changed files with 91 additions and 78 deletions

View File

@@ -6,9 +6,9 @@
// All locale keys for validation *MUST* be fetched prior to this being used as it assumes all keys are fetched first
// Add any new keys used to the block in locale.js=>commonKeysEditForm
import dayjs from "dayjs";
import locale from "./locale";
import _ from "../libs/lodash.min.js";
//import v.$dayjs from "v.$dayjs";
//import v.$gzlocale from "./v.$gzlocale";
//import _ from "../libs/lodash.min.js";
import errorHandler from "./errorhandler";
function isEmpty(o) {
@@ -45,7 +45,7 @@ function isNumber(n) {
//
function getControl(v, ref) {
var ctrl = v.$refs[ref];
return ctrl;
}
@@ -75,52 +75,12 @@ function getControlLabel(ctrl) {
}
export default {
///////////////////////////////
// SERVER ERRORS
//
Server(v, ref) {
//check if any errors and short circuit if none
//check if this control is mentioned at all and short circuit return false if not
var ctrl = getControl(v, ref);
if(typeof ctrl == 'undefined'){
return false;
}
//It IS in the list of error controls:
//check if this control has been changed at all, I guess being here might mean it has?
//If this control is dirty / was changed from the value that was in it when the server returned the error then remove the error for this control from the server error list and return false
//ELSE if this control is unchanged / not dirty
//return the error(s) properly localized
//IF user modifies field in any way then this rule must be cleared
//Some rules are for the entire object so...???
//maybe need a form control that is just about rules or something
return false;
// // "ErrorRequiredFieldEmpty": "{0} is a required field. Please enter a value for {0}",
// var err = locale.get("ErrorRequiredFieldEmpty");
// var fieldName = getControlLabel(ctrl);
// 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
//
Required(v, ref) {
var ctrl = getControl(v, ref);
if(typeof ctrl == 'undefined'){
if (typeof ctrl == "undefined") {
return false;
}
@@ -130,11 +90,11 @@ export default {
}
// "ErrorRequiredFieldEmpty": "{0} is a required field. Please enter a value for {0}",
var err = locale.get("ErrorRequiredFieldEmpty");
var err = v.$gzlocale.get("ErrorRequiredFieldEmpty");
var fieldName = getControlLabel(ctrl);
err = _.replace(err, "{0}", fieldName);
err = v.$_.replace(err, "{0}", fieldName);
//lodash replace only replaces first instance so need to do it twice
err = _.replace(err, "{0}", fieldName);
err = v.$_.replace(err, "{0}", fieldName);
return err;
},
///////////////////////////////
@@ -142,7 +102,7 @@ export default {
//
MaxLength(v, ref, max) {
var ctrl = getControl(v, ref);
if(typeof ctrl == 'undefined'){
if (typeof ctrl == "undefined") {
return false;
}
@@ -151,14 +111,13 @@ export default {
return false;
}
if (value.length > max) {
//get the localized rule text
// "ErrorFieldLengthExceeded": "{0} can not exceed {1} characters.",
var err = locale.get("ErrorFieldLengthExceeded");
var err = v.$gzlocale.get("ErrorFieldLengthExceeded");
var fieldName = getControlLabel(ctrl);
err = _.replace(err, "{0}", fieldName);
err = _.replace(err, "{1}", max);
err = v.$_.replace(err, "{0}", fieldName);
err = v.$_.replace(err, "{1}", max);
return err;
} else {
return false;
@@ -174,12 +133,12 @@ export default {
// AFTER
After(v, refStart, refEnd) {
var ctrlStart = getControl(v, refStart);
if(typeof ctrlStart == 'undefined'){
if (typeof ctrlStart == "undefined") {
return false;
}
var ctrlEnd = getControl(v, refEnd);
if(typeof ctrlEnd == 'undefined'){
if (typeof ctrlEnd == "undefined") {
return false;
}
@@ -192,9 +151,9 @@ export default {
if (isEmpty(valueEnd)) {
return false;
}
valueStart = dayjs(valueStart);
valueEnd = dayjs(valueEnd);
valueStart = v.$dayjs(valueStart);
valueEnd = v.$dayjs(valueEnd);
// if either is not valid.
if (!valueStart || !valueEnd) {
@@ -203,7 +162,7 @@ export default {
if (valueStart.isAfter(valueEnd)) {
// "ErrorStartDateAfterEndDate": "Start date must be earlier than stop / end date",
var err = locale.get("ErrorStartDateAfterEndDate");
var err = v.$gzlocale.get("ErrorStartDateAfterEndDate");
return err;
} else {
return false;
@@ -213,9 +172,8 @@ export default {
// INTEGER
//
Integer(v, ref) {
var ctrl = getControl(v, ref);
if(typeof ctrl == 'undefined'){
if (typeof ctrl == "undefined") {
return false;
}
@@ -224,28 +182,24 @@ export default {
return false;
}
if(isInt(value)){
if (isInt(value)) {
return false;
}
// "ErrorFieldValueNotInteger": "Value must be an integer"
var err = locale.get("ErrorFieldValueNotInteger");
var err = v.$gzlocale.get("ErrorFieldValueNotInteger");
return err;
},
///////////////////////////////
// DECIMAL
// Basically anything that can be a number is valid
// Basically anything that can be a number is valid
Decimal(v, ref) {
//TODO: Handle commas and spaces in numbers
//as per locale rules for numbers
//as per v.$gzlocale rules for numbers
var ctrl = getControl(v, ref);
if(typeof ctrl == 'undefined'){
if (typeof ctrl == "undefined") {
return false;
}
@@ -254,15 +208,74 @@ export default {
return false;
}
if(isNumber(value)){
if (isNumber(value)) {
return false;
}
// "ErrorFieldValueNotDecimal": "Value must be a number"
var err = locale.get("ErrorFieldValueNotDecimal");
var err = v.$gzlocale.get("ErrorFieldValueNotDecimal");
return err;
},
///////////////////////////////
// SERVER ERRORS
// Process and return server errors if any for form and field specified
ServerErrors(v, ref) {
//OK, this is sketchy a bit but seems to work
//If it returns a value that is displayed as an error in the field (hides any rule errors but whatever)
//Tested and confirmed that If I make a change to the underlying data property that is housing the data used here to determine if a field has an error, and remove that error then it instantly resolves in the UI and removes the message so that's good!
// - so if we store the server errors in the data() property and edit it the changes to (for example remove a rule), the changes will be reflected instantly
//Vuetify seems to prioritize messages over validation rule messages or perhaps it's set to display only one at a time no matter what value is set for error-count
//What I need is each field needs to bind here
//This code needs to determine if there are any errors for the field in question,
//and return the appropriate string of text
//Then eventually this needs to go into gzvalidate as part of *it's* code so I can just easily call that shit from anywhere
//To determine: how to detect the field has been edited and is dirty and so remove the server error message for that field?
//Have a think about this: is it better to just show all server errors in their own place instead?
// - how many are field related and how many are general?
//- what is best for the end user and least confusing? (probably error with field unless it's general then at top and cleared instantly when they modify the field in question)
//IN ADDITION: on submit needs to clear serverErrors
/*
Steps to completion:
On submit show server errors for fields in their fields, show general errors at the top of form in an error box
When a field is edited any *server* errors must clear for that field right away
When the form is submitted all server errors cleared if any from previous submit
//example error when submit when there are no roles set at all (blank)
//{"error":{"code":"2200","details":[{"code":"2200","message":"","target":"roles","error":"VALIDATION_FAILED"}],"message":"Object did not pass validation"}}
*/
debugger;
if (v.$_.isEmpty(this.serverErrors)) return [];
if (fieldName == "roles") {
return ["This is an error"];
}
var ctrl = getControl(v, ref);
if (typeof ctrl == "undefined") {
return false;
}
var value = getControlValue(ctrl);
if (isEmpty(value)) {
return false;
}
if (isInt(value)) {
return false;
}
// "ErrorFieldValueNotInteger": "Value must be an integer"
var err = v.$gzlocale.get("ErrorFieldValueNotInteger");
return err;
}
};

View File

@@ -73,7 +73,7 @@
:label="this.$gzlocale.get('WidgetRoles')"
ref="roles"
:rules="[this.$gzv.Integer(this,'roles'),this.$gzv.Required(this,'roles')]"
:error-messages="canHasServerError('roles')"
:error-messages="this.$gzv.ServerErrors(this,'roles')"
required
></v-text-field>
</v-flex>