/// <summary> /// Check mandatory field /// </summary> /// <param name="obj"></param> /// <param name="field"></param> /// <returns></returns> public static string[][] CheckMandatoryFiled(object obj, params string[] field) { ObjectMandatoryField omf = new ObjectMandatoryField(); if (field != null) { foreach (string f in field) { MandatoryField mc = new MandatoryField(); mc.FieldName = f; mc.ControlName = f; mc.MandatoryMessage = f; omf.AddProperty(mc); } } else if (obj != null && obj is object) { PropertyInfo[] props = obj.GetType().GetProperties(); if (props != null) { foreach (PropertyInfo prop in props) { MandatoryField mc = new MandatoryField(); mc.FieldName = prop.Name; mc.ControlName = prop.Name; mc.MandatoryMessage = prop.Name; omf.AddProperty(mc); } } } return(CheckMandatoryFiled(obj, omf)); }
/// <summary> /// Check all proprty in object is null or empty /// </summary> /// <param name="obj"></param> /// <param name="paramIggnoreField"></param> /// <returns></returns> public static bool IsNullAllField(object obj, params string[] paramIggnoreField) { PropertyInfo[] props = obj.GetType().GetProperties(); //Fix bug by Non A. 2/Mar/2012 props = (from m in props where !paramIggnoreField.Contains <string>(m.Name) select m).ToArray <PropertyInfo>(); ObjectMandatoryField f = new ObjectMandatoryField(); for (int i = 0; i < props.Length; i++) { f.AddProperty(props[i].Name, props[i].Name); } string[][] strNullList = CheckMandatoryFiled(obj, f); if (strNullList != null) { if (strNullList.Length > 0) { return(strNullList[0].Length == props.Length); } else { return(false); } } else { return(false); } }
/// <summary> /// Check mandatory field /// </summary> /// <param name="obj"></param> /// <param name="field"></param> /// <param name="return_ctrl"></param> /// <returns></returns> public static string[][] CheckMandatoryFiled(object obj, MandatoryField field, bool return_ctrl = false) { List <string> nullLst = new List <string>(); List <string> nullCtrlLst = new List <string>(); if (field is ListMandatoryField) { ListMandatoryField lfield = field as ListMandatoryField; if (IsNullOrEmpty(obj)) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.FieldName); } else { IList lst = null; if (obj is HttpRequestBase) { lst = new List <string>(); HttpRequestBase req = obj as HttpRequestBase; if (lfield.Rows != null) { foreach (MandatoryField mf in lfield.Rows) { lst.Add(req[mf.FieldName]); } } } else { lst = obj as IList; if (lst.Count <= 0) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.FieldName); } } if (lst.Count > 0) { for (int idx = 0; idx < lst.Count; idx++) { if (lfield.Index != null) { if (idx > lfield.Index) { break; } if (idx != lfield.Index) { continue; } } MandatoryField mf = lfield.Field; if (mf == null && lfield.Rows != null) { if (idx < lfield.Rows.Count) { mf = lfield.Rows[idx]; } } if (mf == null) { break; } string[][] sLst = CheckMandatoryFiled(lst[idx], mf, return_ctrl); if (sLst != null) { if (lfield.Rows != null || lfield.Index != null) { nullLst.AddRange(sLst[0]); if (sLst.Length > 1) { nullCtrlLst.AddRange(sLst[1]); } } else { string txt = string.Format("Row {0} [{1}]", (idx + 1), TextList(sLst[0])); if (field.MandatoryMessage != null) { txt = field.MandatoryMessage + " " + txt; } nullLst.Add(txt); if (sLst.Length > 1) { nullCtrlLst.Add(string.Format("{0}:{1}", (idx + 1), TextList(sLst[1]))); } if (lfield.IsBreakLoop) { break; } } } } } } } else if (field is ObjectMandatoryField) { ObjectMandatoryField ofield = field as ObjectMandatoryField; if (ofield.Properties != null) { if (obj == null) { nullLst.Add(ofield.MandatoryMessage); nullCtrlLst.Add(ofield.ControlName); } else { foreach (MandatoryField mf in ofield.Properties) { PropertyInfo prop = obj.GetType().GetProperty(mf.FieldName); if (prop != null) { object val = prop.GetValue(obj, null); string[][] sLst = CheckMandatoryFiled(val, mf, return_ctrl); if (sLst != null) { nullLst.AddRange(sLst[0]); if (sLst.Length > 1) { nullCtrlLst.AddRange(sLst[1]); } } } } } } } else { if (field.FieldName == null || obj == null) { if (IsNullOrEmpty(obj)) { nullLst.Add(field.MandatoryMessage != null ? field.MandatoryMessage : ""); nullCtrlLst.Add(field.ControlName != null ? field.ControlName : field.FieldName != null ? field.FieldName : ""); } } else { PropertyInfo prop = obj.GetType().GetProperty(field.FieldName); if (prop != null) { object val = prop.GetValue(obj, null); if (IsNullOrEmpty(val)) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.ControlName != null ? field.ControlName : field.FieldName); } } else { if (IsNullOrEmpty(obj)) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.ControlName != null ? field.ControlName : field.FieldName); } } } } if (nullLst.Count > 0) { int length = 1; if (return_ctrl) { length = 2; } string[][] res = new string[length][]; res[0] = nullLst.ToArray(); if (length == 2 && nullCtrlLst.Count > 0) { res[1] = nullCtrlLst.ToArray(); } return(res); } return(null); }