Files
raven/server/AyaNova/biz/RequiredFieldsValidator.cs
2019-06-25 19:15:45 +00:00

52 lines
2.1 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 = FormAvailableFields.FormFields(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
FormField FF = FormFields.Where(x => x.Key == FldLtKey).Single();
if (!string.IsNullOrWhiteSpace(FF.PropertyName))
{
//Now get the actual property name from the available fields using the lt key
string RequiredPropertyName = FF.PropertyName;
//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);
}
}
}
}
}//eoc
}//ens