質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Entity Framework

Entity Frameworkは、.NET Framework 3.5より追加されたデータアクセス技術。正式名称は「ADO.NET Entity Framework」です。データベースエンジンに依存しておらず、データプロバイダの変更のみで様々なデータベースに対応できます。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

ASP.NET MVC Framework

ASP.NET MVC Frameworkは、MVCパターンをベースとした、マイクロソフトのウェブアプリケーション開発用のフレームワークです。

Q&A

解決済

1回答

2794閲覧

ASP.NET MVC5 3つのTimeSpan型の比較で入力検証をつける

blackdifferent

総合スコア25

Entity Framework

Entity Frameworkは、.NET Framework 3.5より追加されたデータアクセス技術。正式名称は「ADO.NET Entity Framework」です。データベースエンジンに依存しておらず、データプロバイダの変更のみで様々なデータベースに対応できます。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

ASP.NET MVC Framework

ASP.NET MVC Frameworkは、MVCパターンをベースとした、マイクロソフトのウェブアプリケーション開発用のフレームワークです。

0グッド

0クリップ

投稿2019/01/09 02:13

度々すみません。
前回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); } } } }   ・・・ }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

すみません。
自分で考えていて実装出来ました。
前回のValidationAttributeの引数を増やすだけで対応出来ました。
以下にコードを載せます。

モデル:

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)] public Nullable<System.TimeSpan> Open { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}"), DataType(System.ComponentModel.DataAnnotations.DataType.Time)] [TimeGreaterThan("Open", "終業時刻は始業時刻より後に設定してください")] public Nullable<System.TimeSpan> Close { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}"), DataType(System.ComponentModel.DataAnnotations.DataType.Time)] [TimeSmallerThan("Open","Close","休憩時間は出社時間より短く設定してください")] 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 TimeSmallerThanAttribute : ValidationAttribute { private string _startTimePropertyName; private string _closeTimePropertyName; // コンストラクタ public TimeSmallerThanAttribute( string startTimePropertyName, string closeTimePropertyName, string errorMsg) { this._startTimePropertyName = startTimePropertyName; this._closeTimePropertyName = closeTimePropertyName; 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_open = validationContent.ObjectType. GetProperty(this._startTimePropertyName); System.Reflection.PropertyInfo propertyInfo_close = validationContent.ObjectType. GetProperty(this._closeTimePropertyName); object propertyValue_open = propertyInfo_open. GetValue(validationContent.ObjectInstance, null); object propertyValue_close = propertyInfo_close. GetValue(validationContent.ObjectInstance, null); if (value == null || propertyValue_open == null || propertyValue_close == null) { return ValidationResult.Success; } else { if ((TimeSpan)value < (TimeSpan)propertyValue_close - (TimeSpan)propertyValue_open) { return ValidationResult.Success; } else { return new ValidationResult(null); } } } } ・・・ }

投稿2019/01/09 02:40

blackdifferent

総合スコア25

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問