Files
raven/server/AyaNova/biz/RequiredFieldsValidator.cs
2019-01-16 19:14:50 +00:00

46 lines
1.8 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 FormTemplate = JArray.Parse(formCustom.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)
{
//get the actual property name
//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"]}
string RequiredPropertyName = FormFields.Where(x => x.Key == FldLtKey).Single().PropertyName;
//use reflection to get the underlying value
object propertyValue = proposedObject.GetType().GetProperty(RequiredPropertyName).GetValue(proposedObject, null);
if (propertyValue == null || string.IsNullOrWhiteSpace(propertyValue.ToString()))
biz.AddError(ValidationErrorType.RequiredPropertyEmpty, FldLtKey);
break;
}
}
}
}//eoc
}//ens