度々すみません。
前回ASP.NET MVC5 TimeSpan型入力値の検証でWeb勤務表の終業時刻の入力検証を付けることが出来たのですが、今度は休憩時間が始業時刻と終業時刻の差より大きくならないよう入力検証を付けたいです(休憩時間<終業時刻ー始業時刻)。これは勤務時間が負にならないようにするための処理です。
前回は2つの値の比較だったのですが、今回は3つの値を読み込んで比較する必要があるため、どうすればいいか悩んでいます。何かアドバイスをお願い致します。
モデル:
namespace Kintai_CS_.Models { public class KintaiModel { public IList<SampleKintai> Kintais { get; set; } public System.DateTime Date { get; set; } public TPersonalInfo PersonalInfo { get; set; } //ミーティング開催日(TCalenderからミーティング開催日を抽出) [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")] public System.DateTime Meeting_date { get; set; } //祝日リスト(TCalenderから祝日だけを抽出) public IList<PublicHolidays> Public_holidays { get; set; } //月別情報 public TMonthlyData MonthlyData { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}"), DataType(System.ComponentModel.DataAnnotations.DataType.Time)] [RegularExpression(@"((([0-1][0-9])|(2[0-3]))(:[0-5][0-9])(:[0-5][0-9])?)", ErrorMessage = "00:00~23:59で入力してください")] public Nullable<System.TimeSpan> Rest_time { get; set; } public string notification { get; set; } } ・・・ public class SampleKintai { public int Id { get; set; } public string Emp_num { get; set; } public System.DateTime Date { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}"), DataType(System.ComponentModel.DataAnnotations.DataType.Time)] //[RegularExpression(@"((([0-1][0-9])|(2[0-3]))(:[0-5][0-9])(:[0-5][0-9])?)", ErrorMessage = "00:00~23:59で入力してください")] public Nullable<System.TimeSpan> Open { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}"), DataType(System.ComponentModel.DataAnnotations.DataType.Time)] //[RegularExpression(@"((([0-1][0-9])|(2[0-3]))(:[0-5][0-9])(:[0-5][0-9])?)", ErrorMessage = "00:00~23:59で入力してください")] [TimeGreaterThan("Open", "終業時刻は始業時刻より後に設定してください")] public Nullable<System.TimeSpan> Close { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}"), DataType(System.ComponentModel.DataAnnotations.DataType.Time)] //[RegularExpression(@"((([0-1][0-9])|(2[0-3]))(:[0-5][0-9])(:[0-5][0-9])?)", ErrorMessage = "00:00~23:59で入力してください")] public Nullable<System.TimeSpan> Rest { get; set; } [DisplayFormat(DataFormatString = "{0:hh\:mm}")] public Nullable<System.TimeSpan> Worktime { get; set; } [DisplayFormat(DataFormatString = "{0:hh\:mm}")] public Nullable<System.TimeSpan> Overtime { get; set; } public int Situation { get; set; } [StringLength(20, ErrorMessage = "※入力は全角20文字以内")] public string Remark { get; set; } } // [AttributeUsage(AttributeTargets.Property)] public class TimeGreaterThanAttribute : ValidationAttribute { private string _startTimePropertyName; // コンストラクタ public TimeGreaterThanAttribute( string startTimePropertyName, string errorMsg) { this._startTimePropertyName = startTimePropertyName; this.ErrorMessage = errorMsg; } public override string FormatErrorMessage(string name) { return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name); } protected override ValidationResult IsValid( object value, ValidationContext validationContent) { // ObjectType はこの属性を付与したプロパティが属する // クラス、即ち上のコードの SampleKintai クラスとなる System.Reflection.PropertyInfo propertyInfo = validationContent.ObjectType. GetProperty(this._startTimePropertyName); object propertyValue = propertyInfo. GetValue(validationContent.ObjectInstance, null); if (value == null || propertyValue == null) { return ValidationResult.Success; } else { if ((TimeSpan)value > (TimeSpan)propertyValue) { return ValidationResult.Success; } else { return new ValidationResult(null); } } } } ・・・ }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。