56 lines
2.6 KiB
C#
56 lines
2.6 KiB
C#
using AyaNova.Models;
|
|
using System.Linq;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
namespace AyaNova.Biz
|
|
{
|
|
//VALIDATE REQUIRED FIELDS THAT ARE NOT CUSTOM
|
|
|
|
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)
|
|
return;
|
|
|
|
//var OuterJson=JObject.Parse(formCustom.Template);
|
|
var FormTemplate = JArray.Parse(formCustom.Template);
|
|
// var FormTemplate=(JArray)OuterJson["template"];
|
|
var FormFields = AyaFormFieldDefinitions.AyaFormFields(formCustom.FormKey);
|
|
// var ThisFormNormalFieldsList = FormFields.Where(x => x.Custom == false).Select(x => x.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
|
|
AyaFormFieldDefinition FF = FormFields.Where(x => x.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)
|
|
{
|
|
//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
|