diff --git a/docs/8.0/ayanova/docs/api-error-codes.md b/docs/8.0/ayanova/docs/api-error-codes.md index 4633fe1e..d5f9dbc9 100644 --- a/docs/8.0/ayanova/docs/api-error-codes.md +++ b/docs/8.0/ayanova/docs/api-error-codes.md @@ -23,7 +23,7 @@ Here are all the API level error codes that can be returned by the API server: | 2201 | Validation error - Field is required but is empty or null | | 2202 | Validation error - Field length exceeded. The limit will be returned in the `message` property of the validation error | | 2203 | Validation error - invalid value. Usually an type mismatch or a logical or business rule mismatch (i.e. only certain values are valid for current state of object) | -| 2204 | Validation error - Customized form property set to required has an empty value | +| 2204 | Validation error - Customized form property is set to required but has an empty value | | 2205 | Validation error - Required property is missing entirely. Usually a development or communications error | | 2206 | Validation error - A text property is required to be unique but an existing record with an identical value was found in the database | | 2207 | Validation error - The start date must be earlier than the end date | diff --git a/server/AyaNova/biz/ApiErrorCode.cs b/server/AyaNova/biz/ApiErrorCode.cs index 5e988a3e..e2b41de8 100644 --- a/server/AyaNova/biz/ApiErrorCode.cs +++ b/server/AyaNova/biz/ApiErrorCode.cs @@ -50,7 +50,7 @@ namespace AyaNova.Biz | 2201 | Validation error - Field is required but is empty or null | | 2202 | Validation error - Field length exceeded. The limit will be returned in the `message` property of the validation error | | 2203 | Validation error - invalid value. Usually an type mismatch or a logical or business rule mismatch (i.e. only certain values are valid for current state of object) | -| 2204 | Validation error - Customized form property set to required has an empty value | +| 2204 | Validation error - Customized form property is set to required but has an empty value | | 2205 | Validation error - Required property is missing entirely. Usually a development or communications error | | 2206 | Validation error - A text property is required to be unique but an existing record with an identical value was found in the database | | 2207 | Validation error - When an object requires a start and end date the start date must be earlier than the end date | diff --git a/server/AyaNova/biz/ApiErrorCodeStockMessage.cs b/server/AyaNova/biz/ApiErrorCodeStockMessage.cs index 9e4eae05..b4fed10a 100644 --- a/server/AyaNova/biz/ApiErrorCodeStockMessage.cs +++ b/server/AyaNova/biz/ApiErrorCodeStockMessage.cs @@ -43,7 +43,7 @@ namespace AyaNova.Biz case ApiErrorCode.VALIDATION_INVALID_VALUE: return "Field is set to a non allowed value"; case ApiErrorCode.VALIDATION_CUSTOM_REQUIRED_EMPTY: - return "Customized form property set to required has an empty value"; + return "Customized form property is set to required but has an empty value"; case ApiErrorCode.VALIDATION_MISSING_PROPERTY: return "Required property is missing entirel"; case ApiErrorCode.VALIDATION_NOT_UNIQUE: diff --git a/server/AyaNova/biz/CustomFieldsValidator.cs b/server/AyaNova/biz/CustomFieldsValidator.cs index b1c370d5..8067c826 100644 --- a/server/AyaNova/biz/CustomFieldsValidator.cs +++ b/server/AyaNova/biz/CustomFieldsValidator.cs @@ -16,9 +16,9 @@ namespace AyaNova.Biz return; - var OuterJson=JObject.Parse(formCustom.Template); + var OuterJson = JObject.Parse(formCustom.Template); //var FormTemplate = JArray.Parse(formCustom.Template); - var FormTemplate=(JArray)OuterJson["template"]; + var FormTemplate = (JArray)OuterJson["template"]; var ThisFormCustomFieldsList = FormAvailableFields.FormFields(formCustom.FormKey).Where(x => x.Custom == true).Select(x => x.Key).ToList(); //If the customFields string is empty then only validation is if any of the fields are required to be filled in @@ -48,12 +48,21 @@ namespace AyaNova.Biz //However the LT field names might be WidgetCustom1 or UserCustom16 so we need to translate by EndsWith var CustomFieldData = JObject.Parse(customFields); - //make sure all the keys are present + //make sure all the *required* keys are present foreach (string iFldKey in ThisFormCustomFieldsList) { - //Now translate the LT field key to the actual customFieldData field key - var InternalCustomFieldName=FormAvailableFields.TranslateLTCustomFieldToInternalCustomFieldName(iFldKey); + //Translate the LT field key to the actual customFieldData field key + var InternalCustomFieldName = FormAvailableFields.TranslateLTCustomFieldToInternalCustomFieldName(iFldKey); + //Check if it's set to required + var isRequired = CustomFieldIsSetToRequired(FormTemplate, iFldKey); + + //if it's not required then we don't care, jump to the next item... + if (!isRequired) + continue; + + //It's required, make sure the key is present and contains data + if (CustomFieldData.ContainsKey(InternalCustomFieldName)) { //validate for now that the custom fields set as required have data in them. Note that we are not validating the sanity of the values, only that they exist @@ -62,28 +71,52 @@ namespace AyaNova.Biz //validate it string CurrentValue = CustomFieldData[InternalCustomFieldName].Value(); - foreach (JObject jo in FormTemplate) + + if (string.IsNullOrWhiteSpace(CurrentValue)) { - if (jo["fld"].Value() == iFldKey) - { - var fldRequired = jo["required"].Value(); - if (fldRequired && string.IsNullOrWhiteSpace(CurrentValue)) - { - biz.AddError(ApiErrorCode.VALIDATION_CUSTOM_REQUIRED_EMPTY, iFldKey); - } - break; - } + biz.AddError(ApiErrorCode.VALIDATION_CUSTOM_REQUIRED_EMPTY, iFldKey); } + // foreach (JObject jo in FormTemplate) + // { + // if (jo["fld"].Value() == iFldKey) + // { + // var fldRequired = jo["required"].Value(); + // if (fldRequired && string.IsNullOrWhiteSpace(CurrentValue)) + // { + // biz.AddError(ApiErrorCode.VALIDATION_CUSTOM_REQUIRED_EMPTY, iFldKey); + // } + // break; + // } + // } + } else { - //This is a serious issue and invalidates all + //This is a serious issue and invalidates all biz.AddError(ApiErrorCode.VALIDATION_MISSING_PROPERTY, iFldKey); } } } + /// + /// Check if field is required + /// + /// + /// + /// + private static bool CustomFieldIsSetToRequired(JArray FormTemplate, string FieldKey) + { + foreach (JObject jo in FormTemplate) + { + if (jo["fld"].Value() == FieldKey) + { + return jo["required"].Value(); + } + } + return false; + } + }//eoc }//ens diff --git a/server/AyaNova/util/Seeder.cs b/server/AyaNova/util/Seeder.cs index ad1b47f4..ac2c37ed 100644 --- a/server/AyaNova/util/Seeder.cs +++ b/server/AyaNova/util/Seeder.cs @@ -527,19 +527,9 @@ namespace AyaNova.Util o.Notes = f.Lorem.Paragraph(); o.Tags = RandomTags(f); - //RANDOM CUSTOM FIELD DATA - //var custDate=GetSomeDateHereThatIsThenMadeIntoText; - - // Template = "{template:[{fld:\"WidgetNotes\",required:\"true\"}" + - // ",{fld:\"WidgetCustom1\",hide:\"false\",required:\"false\", type:\"date\"}" + - // ",{fld:\"WidgetCustom2\",hide:\"false\",required:\"true\", type:\"text\"}" + - // ",{fld:\"WidgetCustom3\",hide:\"false\",required:\"false\", type:\"int\"}" + - // ",{fld:\"WidgetCustom4\",hide:\"false\",required:\"false\", type:\"bool\"}" + - // ",{fld:\"WidgetCustom5\",hide:\"false\",required:\"false\", type:\"decimal\"}" + - // "]" - - + o.CustomFields=@"{c1:""2019-05-01T21:38:07Z"",c2:""Here is some custom field text in position 2"",c3:100,c4:true,c5:5.55}"; + var NewObject = Biz.Create(ServiceProviderProvider.DBContext, o); if (NewObject == null)