Files
raven/server/AyaNova/biz/RequiredFieldsValidator.cs
2021-02-22 21:06:17 +00:00

63 lines
3.2 KiB
C#

using AyaNova.Models;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace AyaNova.Biz
{
//VALIDATE **USER DEFINED** (not stock) REQUIRED FIELDS THAT ARE NOT CUSTOM
//(fields that are stock required are validated on their own not here)
internal static class RequiredFieldsValidator
{
internal static void Validate(BizObject biz, FormCustom formCustom, object proposedObject)
{
//No form custom = no template to check against so nothing to do
if (formCustom == null || string.IsNullOrWhiteSpace(formCustom.Template))
return;
//var OuterJson=JObject.Parse(formCustom.Template);
var FormTemplate = JArray.Parse(formCustom.Template);
// var FormTemplate=(JArray)OuterJson["template"];
var FormFields = Biz.FormFieldOptionalCustomizableReference.FormFieldReferenceList(formCustom.FormKey);
// var ThisFormNormalFieldsList = FormFields.Where(z => z.Custom == false).Select(z => z.Key).ToList();
foreach (JObject jo in FormTemplate)
{
if (jo["required"].Value<bool>() == true)
{
//First get the LT key
var FldLtKey = jo["fld"].Value<string>();
// - e.g.: {template:[{fld:"ltkeyfieldname",hide:"true/false",required:"true/false", type:"bool"},{fld:"ltkeyfieldname",hide:"true/false",required:"true/false", type:"text"]}
//get the FormField object
FormField FF = FormFields.Where(z => z.FieldKey == FldLtKey).Single();
//don't validate custom fields, just skip them
// if (!string.IsNullOrWhiteSpace(FF.PropertyName))//this used to work because there would be no property name but now there is so it doesn't
if (!FF.IsCustomField)
{
//TODO: CHILD COLLECTION MOD
/*
Update RequiredFieldsValidator to look for these period seperated items and navigate through teh collection by name / reflection to step through children and flag errors to include parent
e.g in validation error field name could be "poitems[3].vendorpartnumber" to indicate poitems collection 4th row has error in vendorpartnumber
*/
//Now get the actual property name from the available fields using the lt key
string RequiredPropertyName = FF.FieldKey;
//use reflection to get the underlying value from the proposed object to be saved
object propertyValue = proposedObject.GetType().GetProperty(RequiredPropertyName).GetValue(proposedObject, null);
if (propertyValue == null || string.IsNullOrWhiteSpace(propertyValue.ToString()))
//biz.AddError(ApiErrorCode.VALIDATION_REQUIRED, FldLtKey);//was returning the ltkey in the error but that's not a match to the client end object so instead...
biz.AddError(ApiErrorCode.VALIDATION_REQUIRED, RequiredPropertyName);
}
}
}
}
}//eoc
}//ens