forked from dotnet/dotnet-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequiredAttribute.xml
227 lines (210 loc) · 14.4 KB
/
RequiredAttribute.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<Type Name="RequiredAttribute" FullName="System.ComponentModel.DataAnnotations.RequiredAttribute">
<TypeSignature Language="C#" Value="public class RequiredAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit RequiredAttribute extends System.ComponentModel.DataAnnotations.ValidationAttribute" />
<TypeSignature Language="DocId" Value="T:System.ComponentModel.DataAnnotations.RequiredAttribute" />
<TypeSignature Language="VB.NET" Value="Public Class RequiredAttribute
Inherits ValidationAttribute" />
<TypeSignature Language="F#" Value="type RequiredAttribute = class
 inherit ValidationAttribute" />
<TypeSignature Language="C++ CLI" Value="public ref class RequiredAttribute : System::ComponentModel::DataAnnotations::ValidationAttribute" />
<AssemblyInfo>
<AssemblyName>System.ComponentModel.Annotations</AssemblyName>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.3.0.0</AssemblyVersion>
<AssemblyVersion>4.3.1.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.ComponentModel.DataAnnotations</AssemblyName>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>3.5.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<TypeForwardingChain>
<TypeForwarding From="System.ComponentModel.DataAnnotations" FromVersion="4.0.0.0" To="System.ComponentModel.Annotations" ToVersion="0.0.0.0" FrameworkAlternate="dotnet-uwp-10.0;netcore-2.0" />
</TypeForwardingChain>
<Base>
<BaseTypeName>System.ComponentModel.DataAnnotations.ValidationAttribute</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute FrameworkAlternate="dotnet-uwp-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)]</AttributeName>
<AttributeName Language="F#">[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-3.5">
<AttributeName Language="C#">[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]</AttributeName>
<AttributeName Language="F#">[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)>]</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>Specifies that a data field value is required.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:System.ComponentModel.DataAnnotations.RequiredAttribute> attribute specifies that when a field on a form is validated, the field must contain a value. A validation exception is raised if the property is `null`, contains an empty string (""), or contains only white-space characters.
> [!NOTE]
> If the MVC data model or entity partial class contains a field that is annotated with the <xref:System.ComponentModel.DataAnnotations.RequiredAttribute> attribute, but the page does not contain the property, an error is not raised. Validation occurs only for fields that are submitted to the server.
## Examples
The following example uses the <xref:System.ComponentModel.DataAnnotations.RequiredAttribute> attribute to override the database schema rule that allows a data field to be empty. The example performs the following steps:
- Implements a metadata partial class and the associated metadata class.
- In the associated metadata class, it applies the <xref:System.ComponentModel.DataAnnotations.RequiredAttribute> attribute, which specifies the following requirements:
- The Title data field cannot be empty. If validation fails, the code in the example throws a validation exception and displays an error message. The error message is specified at the time that the attribute is applied to the data field.
- The MiddleName data field cannot be empty. If validation fails, the code in the example throws a validation exception and displays an error message.
:::code language="csharp" source="~/snippets/csharp/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RequiredAttribute/CS/Customer.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RequiredAttribute/VB/Customer.vb" id="Snippet1":::
]]></format>
</remarks>
</Docs>
<Members>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public RequiredAttribute ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberSignature Language="DocId" Value="M:System.ComponentModel.DataAnnotations.RequiredAttribute.#ctor" />
<MemberSignature Language="VB.NET" Value="Public Sub New ()" />
<MemberSignature Language="C++ CLI" Value="public:
 RequiredAttribute();" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.ComponentModel.Annotations</AssemblyName>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.3.0.0</AssemblyVersion>
<AssemblyVersion>4.3.1.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.ComponentModel.DataAnnotations</AssemblyName>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>3.5.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.RequiredAttribute" /> class.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Examples
The following example shows how to use the <xref:System.ComponentModel.DataAnnotations.RequiredAttribute.%23ctor%2A> constructor. If validation fails, the example displays a validation error message that is provided by Dynamic Data.
:::code language="csharp" source="~/snippets/csharp/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RequiredAttribute/CS/Customer.cs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RequiredAttribute/VB/Customer.vb" id="Snippet3":::
]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName="AllowEmptyStrings">
<MemberSignature Language="C#" Value="public bool AllowEmptyStrings { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance bool AllowEmptyStrings" />
<MemberSignature Language="DocId" Value="P:System.ComponentModel.DataAnnotations.RequiredAttribute.AllowEmptyStrings" />
<MemberSignature Language="VB.NET" Value="Public Property AllowEmptyStrings As Boolean" />
<MemberSignature Language="F#" Value="member this.AllowEmptyStrings : bool with get, set" Usage="System.ComponentModel.DataAnnotations.RequiredAttribute.AllowEmptyStrings" />
<MemberSignature Language="C++ CLI" Value="public:
 property bool AllowEmptyStrings { bool get(); void set(bool value); };" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyName>System.ComponentModel.Annotations</AssemblyName>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.3.0.0</AssemblyVersion>
<AssemblyVersion>4.3.1.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.ComponentModel.DataAnnotations</AssemblyName>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[get: System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<get: System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[set: System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<set: System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets or sets a value that indicates whether an empty string is allowed.</summary>
<value>
<see langword="true" /> if an empty string is allowed; otherwise, <see langword="false" />. The default value is <see langword="false" />.</value>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
When you set <xref:System.ComponentModel.DataAnnotations.RequiredAttribute.AllowEmptyStrings%2A> to `true` for a data field, Dynamic Data does not perform validation and transforms the empty string to a `null` value. This value is then passed to the database.
If the database does not allow `null` values, it raises an error. To avoid this error, you must also set the <xref:System.ComponentModel.DataAnnotations.DisplayFormatAttribute.ConvertEmptyStringToNull%2A> to `false`.
]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName="IsValid">
<MemberSignature Language="C#" Value="public override bool IsValid (object value);" FrameworkAlternate="dotnet-uwp-10.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance bool IsValid(object value) cil managed" />
<MemberSignature Language="DocId" Value="M:System.ComponentModel.DataAnnotations.RequiredAttribute.IsValid(System.Object)" />
<MemberSignature Language="VB.NET" Value="Public Overrides Function IsValid (value As Object) As Boolean" />
<MemberSignature Language="F#" Value="override this.IsValid : obj -> bool" Usage="requiredAttribute.IsValid value" />
<MemberSignature Language="C++ CLI" Value="public:
 override bool IsValid(System::Object ^ value);" />
<MemberSignature Language="C#" Value="public override bool IsValid (object? value);" FrameworkAlternate="net-5.0;net-6.0;net-7.0;net-8.0;net-9.0" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.ComponentModel.Annotations</AssemblyName>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.3.0.0</AssemblyVersion>
<AssemblyVersion>4.3.1.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.ComponentModel.DataAnnotations</AssemblyName>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>3.5.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="net-8.0;net-9.0">
<AttributeName Language="C#">[System.Runtime.CompilerServices.NullableContext(2)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.NullableContext(2)>]</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="value" Type="System.Object" />
</Parameters>
<Docs>
<param name="value">The data field value to validate.</param>
<summary>Checks that the value of the required data field is not empty.</summary>
<returns>
<see langword="true" /> if validation is successful; otherwise, <see langword="false" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
If the value is an empty string, the method returns `false` and a validation error is displayed.
]]></format>
</remarks>
<exception cref="T:System.ComponentModel.DataAnnotations.ValidationException">The data field value was <see langword="null" />.</exception>
</Docs>
</Member>
</Members>
</Type>