RequiredAttribute クラス

定義

データ フィールド値が必要であることを指定します。

public ref class RequiredAttribute : System::ComponentModel::DataAnnotations::ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)]
public class RequiredAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public class RequiredAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)>]
type RequiredAttribute = class
    inherit ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)>]
type RequiredAttribute = class
    inherit ValidationAttribute
Public Class RequiredAttribute
Inherits ValidationAttribute
継承
属性

次の例では、 RequiredAttribute 属性を使用して、データ フィールドを空にできるデータベース スキーマ規則をオーバーライドします。 この例では、次の手順を実行します。

  • メタデータ部分クラスと関連付けられたメタデータ クラスを実装します。

  • 関連付けられたメタデータ クラスでは、次の要件を指定する RequiredAttribute 属性が適用されます。

    • Title データ フィールドを空にすることはできません。 検証が失敗した場合、この例のコードは検証例外をスローし、エラー メッセージを表示します。 エラー メッセージは、属性がデータ フィールドに適用されるときに指定されます。

    • MiddleName データ フィールドを空にすることはできません。 検証が失敗した場合、この例のコードは検証例外をスローし、エラー メッセージを表示します。

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}

public class CustomerMetaData
{
    // Require that the Title is not null.
    // Use custom validation error.
    [Required(ErrorMessage = "Title is required.")]
    public object Title;
    
    // Require that the MiddleName is not null.
    // Use standard validation error.
    [Required()]
    public object MiddleName;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations
Imports System.Globalization

<MetadataType(GetType(CustomerMetaData))> _
Partial Public Class Customer

   
End Class

Public Class CustomerMetaData
    ' Require that the Title is not null.
    ' Use custom validation error.
    <Required(ErrorMessage:="Title is required.")> _
    Public Title As Object

    ' Require that the MiddleName is not null.
    ' Use standard validation error.
    <Required()> _
    Public MiddleName As Object

End Class

注釈

RequiredAttribute属性は、フォーム上のフィールドが検証されるときに、フィールドに値を含める必要があることを指定します。 プロパティが null、空の文字列 ("") を含む場合、または空白文字のみを含む場合、検証例外が発生します。

Note

MVC データ モデルまたはエンティティ部分クラスに、 RequiredAttribute 属性で注釈が付けられたフィールドが含まれているが、ページにプロパティが含まれていない場合、エラーは発生しません。 検証は、サーバーに送信されたフィールドに対してのみ行われます。

コンストラクター

名前 説明
RequiredAttribute()

RequiredAttribute クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
AllowEmptyStrings

空の文字列が許可されるかどうかを示す値を取得または設定します。

ErrorMessage

検証が失敗した場合に検証コントロールに関連付けるエラー メッセージを取得または設定します。

(継承元 ValidationAttribute)
ErrorMessageResourceName

検証が失敗した場合に ErrorMessageResourceType プロパティ値を検索するために使用するエラー メッセージ リソース名を取得または設定します。

(継承元 ValidationAttribute)
ErrorMessageResourceType

検証が失敗した場合にエラー メッセージ検索に使用するリソースの種類を取得または設定します。

(継承元 ValidationAttribute)
ErrorMessageString

ローカライズされた検証エラー メッセージを取得します。

(継承元 ValidationAttribute)
RequiresValidationContext

属性に検証コンテキストが必要かどうかを示す値を取得します。

(継承元 ValidationAttribute)
TypeId

派生クラスで実装されている場合は、この Attributeの一意の識別子を取得します。

(継承元 Attribute)

メソッド

名前 説明
Equals(Object)

このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。

(継承元 Attribute)
FormatErrorMessage(String)

エラーが発生したデータ フィールドに基づいて、エラー メッセージに書式設定を適用します。

(継承元 ValidationAttribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
GetValidationResult(Object, ValidationContext)

現在の検証属性に関して、指定した値が有効かどうかを確認します。

(継承元 ValidationAttribute)
IsDefaultAttribute()

派生クラスでオーバーライドされた場合、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
IsValid(Object, ValidationContext)

現在の検証属性に関して、指定した値を検証します。

(継承元 ValidationAttribute)
IsValid(Object)

必要なデータ フィールドの値が空ではないことを確認します。

Match(Object)

派生クラスでオーバーライドされた場合、このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
Validate(Object, String)

指定したオブジェクトを検証します。

(継承元 ValidationAttribute)
Validate(Object, ValidationContext)

指定したオブジェクトを検証します。

(継承元 ValidationAttribute)

明示的なインターフェイスの実装

名前 説明
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

インターフェイスの型情報を取得するために使用できるオブジェクトの型情報を取得します。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象