/Orchard.Web/Modules/Orchard.Projections/FilterEditors/Forms/DateTimeFormValidation.cs

https://bitbucket.org/oleg_ator/orchard.test
C# | 89 lines | 70 code | 15 blank | 4 comment | 34 complexity | 3e08c23612a7212446f5cb7c11b1c86e MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using Orchard.Forms.Services;
  5. using Orchard.Localization;
  6. namespace Orchard.Projections.FilterEditors.Forms {
  7. public class DateTimeFilterFormValidation : FormHandler {
  8. public Localizer T { get; set; }
  9. private static readonly Regex _dateRegEx = new Regex(@"(\d{1,4}(\-\d{1,2}(\-\d{1,2}\s*(\d{1,2}(:\d{1,2}(:\d{1,2})?)?)?)?)?)|(\{.*\})");
  10. public override void Validating(ValidatingContext context) {
  11. if (context.FormName == DateTimeFilterForm.FormName) {
  12. var isRange = new[] {"Between", "NotBetween"}.Contains(context.ValueProvider.GetValue("Operator").AttemptedValue);
  13. var min = context.ValueProvider.GetValue("Min");
  14. var max = context.ValueProvider.GetValue("Max");
  15. var value = context.ValueProvider.GetValue("Value");
  16. var valueType = context.ValueProvider.GetValue("ValueType");
  17. // validating mandatory values
  18. if (isRange) {
  19. if (min == null || String.IsNullOrWhiteSpace(min.AttemptedValue)) {
  20. context.ModelState.AddModelError("Min", T("The field {0} is required.", T("Min").Text).Text);
  21. }
  22. if (max == null || String.IsNullOrWhiteSpace(max.AttemptedValue)) {
  23. context.ModelState.AddModelError("Max", T("The field {0} is required.", T("Max").Text).Text);
  24. }
  25. }
  26. else {
  27. if (min == null || String.IsNullOrWhiteSpace(value.AttemptedValue)) {
  28. context.ModelState.AddModelError("Value", T("The field {0} is required.", T("Value").Text).Text);
  29. }
  30. }
  31. if (!context.ModelState.IsValid) {
  32. return;
  33. }
  34. // validating data type
  35. if (valueType.AttemptedValue == "0") {
  36. // A date
  37. if(isRange) {
  38. if(!_dateRegEx.IsMatch(min.AttemptedValue) && !IsToken(min.AttemptedValue)) {
  39. context.ModelState.AddModelError("Min", T("The field {0} should contain a valid date (YYYY-MM-DD hh:mm:ss)", T("Min").Text).Text);
  40. }
  41. if (!_dateRegEx.IsMatch(max.AttemptedValue) && !IsToken(max.AttemptedValue)) {
  42. context.ModelState.AddModelError("Max", T("The field {0} should contain a valid date (YYYY-MM-DD hh:mm:ss)", T("Max").Text).Text);
  43. }
  44. }
  45. else {
  46. if (!_dateRegEx.IsMatch(value.AttemptedValue) && !IsToken(value.AttemptedValue)) {
  47. context.ModelState.AddModelError("Value", T("The field {0} should contain a valid date (YYYY-MM-DD hh:mm:ss)", T("Value").Text).Text);
  48. }
  49. }
  50. }
  51. else {
  52. // An offset
  53. int number;
  54. if (isRange) {
  55. if (!Int32.TryParse(min.AttemptedValue, out number) && !IsToken(min.AttemptedValue)) {
  56. context.ModelState.AddModelError("Min", T("The field {0} must be a valid number.", T("Min").Text).Text);
  57. }
  58. if (!Int32.TryParse(max.AttemptedValue, out number) && !IsToken(max.AttemptedValue)) {
  59. context.ModelState.AddModelError("Max", T("The field {0} must be a valid number.", T("Max").Text).Text);
  60. }
  61. }
  62. else {
  63. if (!Int32.TryParse(value.AttemptedValue, out number) && !IsToken(value.AttemptedValue)) {
  64. context.ModelState.AddModelError("Value", T("The field {0} must be a valid number.", T("Value").Text).Text);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. private bool IsToken(string value) {
  71. return value.StartsWith("{") && value.EndsWith("}");
  72. }
  73. }
  74. }