Пример #1
0
        public static string SubmittedValue(this TemplateFieldViewModel field, FormCollection form)
        {
            var    fType = field.FieldType.ToString().ToTitleCase();
            string value = "";

            switch (field.FieldType)
            {
            case Constants.TemplateFieldType.MATRIX:
                StringBuilder builder = new StringBuilder();

                for (int x = 0; x < 100; x++)
                {
                    value = form.SubmittedFieldValue(field.DomId, "[" + x + "].RadioButton");

                    if (value == null)
                    {
                        break;
                    }

                    if (x == 0)
                    {
                        builder.Append(value);
                    }
                    else
                    {
                        builder.Append(Constants.Form_Option_Split + value);
                    }
                }

                value = builder.ToString();

                break;

            case Constants.TemplateFieldType.BMI:
                var bmi = new BMIViewModel
                {
                    Weight = form.SubmittedFieldValue(field.DomId, "Weight"),
                    Height = form.SubmittedFieldValue(field.DomId, "Height"),
                };

                if (bmi.Weight == "" || bmi.Height == "")
                {
                    value = "";
                }
                else
                {
                    value = bmi.ToJson();
                }

                break;

            case Constants.TemplateFieldType.NRICPICKER:
                string icNumber = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                //string icFirstDigit = form.SubmittedFieldValue(field.DomId, "FirstDigit");
                //  string icLastDigit = form.SubmittedFieldValue(field.DomId, "LastDigit");

                value = icNumber;
                break;

            case Constants.TemplateFieldType.EMAIL:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                break;

            case Constants.TemplateFieldType.ADDRESS:
                var address = new AddressViewModel
                {
                    Blk           = form.SubmittedFieldValue(field.DomId, "Blk"),
                    Unit          = form.SubmittedFieldValue(field.DomId, "Unit"),
                    StreetAddress = form.SubmittedFieldValue(field.DomId, "StreetAddress"),
                    ZipCode       = form.SubmittedFieldValue(field.DomId, "ZipCode"),
                };

                if (address.Blk.IsEmptyOrWhiteSpace() && address.StreetAddress.IsEmptyOrWhiteSpace())
                {
                    value = "";
                }
                else
                {
                    value = address.ToJson();
                }
                break;

            case Constants.TemplateFieldType.BIRTHDAYPICKER:
                var day   = form.SubmittedFieldValue(field.DomId, "Day");
                var month = form.SubmittedFieldValue(field.DomId, "Month");
                var year  = form.SubmittedFieldValue(field.DomId, "Year");

                if (day.IsNullOrEmpty() && month.IsNullOrEmpty() && year.IsNullOrEmpty())
                {
                    value = "";
                }
                else
                {
                    DateTime date = new DateTime(Int32.Parse(year), Int32.Parse(month), Int32.Parse(day));
                    value = date.ToString("dd/M/yyyy hh:mm:ss tt");

                    /*
                     * var dateValue = "{0}-{1}-{2}".FormatWith(month, day, year);
                     * //var format = new string[] { "M-dd-yyyy" };
                     * DateTime date;
                     * if (DateTime.TryParseExact(dateValue, "M-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out date))
                     * {
                     *  value = date.ToString("dd/M/yyyy hh:mm:ss tt");
                     * }
                     * else
                     * {
                     *  value = "";
                     * }*/
                }
                break;

            case Constants.TemplateFieldType.CHECKBOX:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                if (!value.IsNullOrEmpty())
                {
                    string newValue = "";
                    foreach (var option in field.Options.Split(Constants.Form_Option_Split))
                    {
                        if (value.Contains(option))
                        {
                            newValue += Constants.Form_Option_Split_Concate + option;
                        }
                    }


                    if (value.Contains("OthersOption"))
                    {
                        string othersOptionValue = form.SubmittedFieldValue(field.DomId, "OthersOption");
                        newValue += Constants.Form_Option_Split_Concate + othersOptionValue;
                    }


                    if (newValue.Substring(0, 1).Equals(Constants.Form_Option_Split_Concate))
                    {
                        newValue = newValue.Remove(0, 1);
                    }

                    value = newValue;
                }
                break;

            case Constants.TemplateFieldType.PHONE:
                var area   = form.SubmittedFieldValue(field.DomId, "AreaCode");
                var number = form.SubmittedFieldValue(field.DomId, "Number");
                if (string.IsNullOrEmpty(area) && string.IsNullOrEmpty(number))
                {
                    value = "";
                }
                else
                {
                    value = "{0}-{1}".FormatWith(area, number);
                }
                break;

            case Constants.TemplateFieldType.DROPDOWNLIST:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                break;

            case Constants.TemplateFieldType.RADIOBUTTON:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                if (!value.IsNullOrEmpty() && value.Equals("OthersOption"))
                {
                    value = form.SubmittedFieldValue(field.DomId, "OthersOption");
                }
                break;

            case Constants.TemplateFieldType.FULLNAME:
                var fName    = form.SubmittedFieldValue(field.DomId, "FirstName");
                var lName    = form.SubmittedFieldValue(field.DomId, "LastName");
                var initials = form.SubmittedFieldValue(field.DomId, "Initials");
                if (string.IsNullOrEmpty(fName) && string.IsNullOrEmpty(lName))
                {
                    value = "";
                }
                else
                {
                    value = "{0} {1} {2}".FormatWith(fName, initials, lName);
                }
                break;

            case Constants.TemplateFieldType.TEXTAREA:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                break;

            case Constants.TemplateFieldType.TEXTBOX:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                break;

            case Constants.TemplateFieldType.TEXTBOXNUMBER:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                break;

            case Constants.TemplateFieldType.SIGNATURE:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                break;

            case Constants.TemplateFieldType.DOCTORMEMO:
                value = form.SubmittedFieldValue(field.DomId, "TextArea");
                break;

            case Constants.TemplateFieldType.FILEPICKER:
                //HttpPostedFile file = HttpContext.Current.Request.Files[SubmittedFieldName(field.DomId, fType.ToTitleCase())];
                //value = "";
                //if (file != null && file.ContentLength > 0)
                //{
                //    var extension = Path.GetExtension(file.FileName);

                //    var valueObject = new FileValueObject()
                //    {
                //        FileName = file.FileName,
                //        SaveName = Guid.NewGuid().ToString() + extension,
                //        SavePath = WebConfig.Get("filesavepath"),
                //        IsSavedInCloud = UtilityHelper.UseCloudStorage(),
                //        Extension = extension
                //    };

                //    value = valueObject.ToJson();
                //}
                break;
            }

            return(value);
        }
Пример #2
0
        public static bool SubmittedValueIsValid(this TemplateFieldViewModel field, FormCollection form)
        {
            var    fType = field.FieldType.ToString().ToTitleCase();
            string value = "";

            switch (field.FieldType)
            {
            case Constants.TemplateFieldType.TEXTBOXNUMBER:
                string textnumber = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                if (string.IsNullOrEmpty(textnumber))
                {
                    return(true);
                }
                return(textnumber.IsNumeric());

            case Constants.TemplateFieldType.NRICPICKER:
                string icNumber = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                // string icFirstDigit = form.SubmittedFieldValue(field.DomId, "FirstDigit");
                // string icLastDigit = form.SubmittedFieldValue(field.DomId, "LastDigit");

                return(NricChecker.IsNRICValid(icNumber));

            case Constants.TemplateFieldType.CHECKBOX:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                if (!value.IsNullOrEmpty() && value.Contains("OthersOption"))
                {
                    string othersOptionValue = form.SubmittedFieldValue(field.DomId, "OthersOption");
                    if (othersOptionValue.IsNullOrEmpty())
                    {
                        return(false);
                    }
                }
                return(true);

            case Constants.TemplateFieldType.RADIOBUTTON:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                if (!value.IsNullOrEmpty() && value.Equals("OthersOption"))
                {
                    value = form.SubmittedFieldValue(field.DomId, "OthersOption");
                    if (value.IsNullOrEmpty())
                    {
                        return(false);
                    }
                }
                return(true);

            case Constants.TemplateFieldType.EMAIL:
                value = form.SubmittedFieldValue(field.DomId, fType.ToTitleCase());
                if (value.IsNullOrEmpty())
                {
                    return(true);
                }
                return(value.IsValidEmail());

            case Constants.TemplateFieldType.ADDRESS:
                return(true);

            case Constants.TemplateFieldType.PHONE:
                var area   = form.SubmittedFieldValue(field.DomId, "AreaCode");
                var number = form.SubmittedFieldValue(field.DomId, "Number");
                if (area.IsNullOrEmpty() && number.IsNullOrEmpty())
                {
                    return(true);
                }
                else if ((area.IsNullOrEmpty() && !number.IsNullOrEmpty()) || (!area.IsNullOrEmpty() && number.IsNullOrEmpty()))
                {
                    return(false);
                }
                else
                {
                    return(area.IsNumeric() && number.IsNumeric());
                }

            case Constants.TemplateFieldType.BIRTHDAYPICKER:
                var day   = form.SubmittedFieldValue(field.DomId, "Day");
                var month = form.SubmittedFieldValue(field.DomId, "Month");
                var year  = form.SubmittedFieldValue(field.DomId, "Year");

                if (day.IsNullOrEmpty() && month.IsNullOrEmpty() && year.IsNullOrEmpty())
                {
                    return(true);
                }

                try
                {
                    if (!string.IsNullOrEmpty(day))
                    {
                        int dayValue = Int32.Parse(day);
                        if (dayValue <= 0 || dayValue >= 32)
                        {
                            return(false);
                        }
                    }

                    if (!string.IsNullOrEmpty(month))
                    {
                        int monthValue = Int32.Parse(month);
                        if (monthValue <= 0 || monthValue >= 13)
                        {
                            return(false);
                        }
                    }

                    if (!string.IsNullOrEmpty(year))
                    {
                        int yearValue = Int32.Parse(year);
                        if (yearValue <= 1916 || yearValue >= 2999)
                        {
                            return(false);
                        }
                    }


                    return(true);
                }

                catch (FormatException)
                {
                    return(false);
                }

            /*
             * var dateValue = "{0}-{1}-{2}".FormatWith(month, day, year);
             * var format = new string[] { "M-dd-yyyy" };
             * DateTime date;
             * return DateTime.TryParseExact(dateValue, "M-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out date);
             */
            case Constants.TemplateFieldType.FILEPICKER:
                HttpPostedFile file            = HttpContext.Current.Request.Files[SubmittedFieldName(field.DomId, fType.ToTitleCase())];
                var            maxSize         = field.MaxFileSize * 1024;
                var            minSize         = field.MinFileSize * 1024;
                var            validExtensions = field.ValidFileExtensions;

                if (file != null && file.ContentLength > 0)
                {
                    var extension = System.IO.Path.GetExtension(file.FileName);
                    // check filesize is within range of Max and Min
                    if (!(file.ContentLength >= minSize && file.ContentLength <= maxSize))
                    {
                        return(false);
                    }

                    // check file extension is valid
                    if (!validExtensions.IsNullOrEmpty())
                    {
                        var  validExtensionArr = validExtensions.Split(",").Select(ext => ext.Trim()).ToList();
                        bool isValidExt        = false;
                        foreach (var ext in validExtensionArr)
                        {
                            var updatedExt = ext;
                            if (!ext.StartsWith("."))
                            {
                                updatedExt = "." + ext;
                            }

                            if (updatedExt.IsTheSameAs(extension))
                            {
                                isValidExt = true;
                            }
                        }

                        return(isValidExt);
                    }
                }

                return(true);
            }

            return(true);
        }