Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="drv"></param>
        /// <param name="isValid"></param>
        /// <param name="fieldName">to reference another field to be invalidated as well, comma delimit with no spaces (yeah i know it's hacky)</param>
        /// <param name="doValidation"> </param>
        /// <param name="expression"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        protected bool ValidateGeneric(DataRowView drv, ref bool isValid, string fieldName, bool doValidation = true, string expression = "'?' != ''", string message = "Required")
        {
            //if (!drv.IsDirty()) return (true);

            string[] flds = fieldName.Split(',');
            if (flds.Length == 2)
            {
                fieldName = flds[0];
            }

            bool isvalid = true;

            if (doValidation)
            {
                isvalid = StringEvaluator.EvalToBool(expression.Replace("?", drv[fieldName].ToString()));
            }
            isValid = isValid && isvalid;

            //if error, set error and notify UI, otherwise clear any existing errors
            if (!isvalid)
            {
                if (flds.Length == 2)
                {
                    drv.SetColumnError(flds[1], message);
                }
                drv.SetColumnError(fieldName, message); //nugget: setting errors on ADO.Net DataRowView fields which propogate all the way back up to little red boxes & tooltips on the corresponding UI widgets
            }
            else
            {
                if (flds.Length == 2)
                {
                    drv.ClearColumnError(flds[1]);
                }
                drv.ClearColumnError(fieldName);
            }

            return(isvalid);
        }