Example #1
0
        /// <summary>
        /// This method fires when the validation for the deDischargeDate DevExpress
        /// Bootstrap DateEdit fires and it validates that control's value
        /// </summary>
        /// <param name="sender">The deDischargeDate DateEdit</param>
        /// <param name="e">ValidationEventArgs</param>
        protected void deDischargeDate_Validation(object sender, ValidationEventArgs e)
        {
            //Get the discharge date, enrollment date, and discharge reason
            DateTime?dischargeDate   = (deDischargeDate.Value == null ? (DateTime?)null : Convert.ToDateTime(deDischargeDate.Value));
            DateTime?enrollmentDate  = (deEnrollmentDate.Value == null ? (DateTime?)null : Convert.ToDateTime(deEnrollmentDate.Value));
            string   dischargeReason = (ddDischargeReason.Value == null ? null : ddDischargeReason.Value.ToString());

            //Perform the validation
            if (dischargeDate.HasValue == false && dischargeReason != null)
            {
                e.IsValid   = false;
                e.ErrorText = "Discharge Date is required if you have a Discharge Reason!";
            }
            else if (dischargeDate.HasValue && enrollmentDate.HasValue == false)
            {
                e.IsValid   = false;
                e.ErrorText = "Enrollment Date must be entered before the Discharge Date!";
            }
            else if (dischargeDate.HasValue && dischargeDate.Value < enrollmentDate.Value)
            {
                e.IsValid   = false;
                e.ErrorText = "Discharge Date must be after the Enrollment Date!";
            }
            else if (dischargeDate.HasValue && dischargeDate > DateTime.Now)
            {
                e.IsValid   = false;
                e.ErrorText = "Discharge Date cannot be in the future!";
            }
            else if (dischargeDate.HasValue)
            {
                //Get the child and program pks
                int childPK   = Convert.ToInt32(hfChildPK.Value);
                int programPK = Convert.ToInt32(hfProgramFK.Value);

                //Only continue if this is an edit
                if (childPK > 0)
                {
                    using (PyramidContext context = new PyramidContext())
                    {
                        //Validate the discharge date against other forms' dates
                        var formValidationResults = context.spValidateEnrollmentDischargeDates(childPK,
                                                                                               programPK, (DateTime?)null, dischargeDate).ToList();

                        //If there are results, the discharge date is invalid
                        if (formValidationResults.Count > 0)
                        {
                            e.IsValid   = false;
                            e.ErrorText = "Discharge Date is invalid, see notification message for details!";

                            //Create a message that contains the forms that would be invalidated
                            string message = "The Discharge Date would invalidate these records if changed to that date:<br/><br/>";
                            foreach (spValidateEnrollmentDischargeDates_Result invalidForm in formValidationResults)
                            {
                                message += invalidForm.ObjectName + " (" + invalidForm.ObjectDate.Value.ToString("MM/dd/yyyy") + ")";
                                message += "<br/>";
                            }

                            //Show the message
                            ValidationMessageToDisplay = message;
                        }
                    }
                }
            }
        }