33 using System ;
44 using System . Collections . Generic ;
55 using System . ComponentModel . DataAnnotations ;
6+ using System . Linq ;
67 using LearningHub . Nhs . WebUI . Helpers ;
78
89 /// <summary>
@@ -13,32 +14,79 @@ public class AccountCreationDateViewModel : AccountCreationViewModel, IValidatab
1314 /// <summary>
1415 /// Gets or sets the Day.
1516 /// </summary>
16- public int ? Day { get ; set ; }
17+ public string Day { get ; set ; }
18+
19+ /// <summary>
20+ /// Gets or sets the Day Field.
21+ /// </summary>
22+ public int ? DayInput { get ; set ; }
1723
1824 /// <summary>
1925 /// Gets or sets the Country.
2026 /// </summary>
21- public int ? Month { get ; set ; }
27+ public string Month { get ; set ; }
28+
29+ /// <summary>
30+ /// Gets or sets the Month input.
31+ /// </summary>
32+ public int ? MonthInput { get ; set ; }
2233
2334 /// <summary>
2435 /// Gets or sets the Year.
2536 /// </summary>
26- public int ? Year { get ; set ; }
37+ public string Year { get ; set ; }
38+
39+ /// <summary>
40+ /// Gets or sets YearInput.
41+ /// </summary>
42+ public int ? YearInput { get ; set ; }
2743
2844 /// <summary>
2945 /// Gets or sets the GetDate.
3046 /// </summary>
3147 /// <returns>DateTime.</returns>
3248 public DateTime ? GetDate ( )
3349 {
34- return ( this . Day . HasValue && this . Month . HasValue && this . Year . HasValue ) ? new DateTime ( this . Year ! . Value , this . Month ! . Value , this . Day ! . Value ) : ( DateTime ? ) null ;
50+ return ( this . DayInput . HasValue && this . MonthInput . HasValue && this . YearInput . HasValue ) ? new DateTime ( this . YearInput ! . Value , this . MonthInput ! . Value , this . DayInput ! . Value ) : ( DateTime ? ) null ;
3551 }
3652
3753 /// <inheritdoc/>
3854 public IEnumerable < ValidationResult > Validate ( ValidationContext validationContext )
3955 {
40- return DateValidator . ValidateDate ( this . Day , this . Month , this . Year , "valid start date" )
41- . ToValidationResultList ( nameof ( this . Day ) , nameof ( this . Month ) , nameof ( this . Year ) ) ;
56+ var validationResults = new List < ValidationResult > ( ) ;
57+ int parsedDay = 0 ;
58+ int parsedMonth = 0 ;
59+ int parsedYear = 0 ;
60+
61+ if ( ! string . IsNullOrWhiteSpace ( this . Day ) && ! int . TryParse ( this . Day , out parsedDay ) )
62+ {
63+ validationResults . Add ( new ValidationResult (
64+ $ "The value '{ this . Day } ' is not valid for Day.", new [ ] { nameof ( this . Day ) } ) ) ;
65+ }
66+
67+ if ( ! string . IsNullOrWhiteSpace ( this . Month ) && ! int . TryParse ( this . Month , out parsedMonth ) )
68+ {
69+ validationResults . Add ( new ValidationResult (
70+ $ "The value '{ this . Month } ' is not valid for Month.", new [ ] { nameof ( this . Month ) } ) ) ;
71+ }
72+
73+ if ( ! string . IsNullOrWhiteSpace ( this . Year ) && ! int . TryParse ( this . Year , out parsedYear ) )
74+ {
75+ validationResults . Add ( new ValidationResult (
76+ $ "The value '{ this . Year } ' is not valid for Year.", new [ ] { nameof ( this . Year ) } ) ) ;
77+ }
78+
79+ if ( validationResults . Count > 0 )
80+ {
81+ return validationResults ;
82+ }
83+
84+ this . DayInput = parsedDay ;
85+ this . MonthInput = parsedMonth ;
86+ this . YearInput = parsedYear ;
87+
88+ return DateValidator . ValidateDate ( this . DayInput , this . MonthInput , this . YearInput , "valid start date" )
89+ . ToValidationResultList ( nameof ( this . Day ) , nameof ( this . Month ) , nameof ( this . Year ) ) ;
4290 }
4391 }
4492}
0 commit comments