When bound to a nullable GUID, the DropDownList invokes validation and shows validation messages as soon as the view loads.
Expected: the same behavior as with nullable int - validation is to be triggered by the form submission or selection from the dropdown.
Worked as expected in 1.2.0.
Reproducible:
@
using
Telerik.Blazor.Components.DropDownList
@
using
System.ComponentModel.DataAnnotations
<EditForm Model=
"@person"
OnValidSubmit=
"@HandleValidSubmit"
>
<DataAnnotationsValidator />
<ValidationSummary />
<p
class
=
"gender"
>
Gender: <TelerikDropDownList @bind-Value=
"person.Gender"
DefaultItem=
"@ddlHint"
Data=
"@genders"
TextField=
"MyTextField"
ValueField=
"MyValueField"
>
</TelerikDropDownList>
<ValidationMessage For=
"@(() => person.Gender)"
></ValidationMessage>
</p>
<button type=
"submit"
>Submit</button>
</EditForm>
@code {
// Usually the model classes would be in different files
public
class
Person
{
[Required(ErrorMessage =
"Gender is mandatory."
)]
//the value field in the dropdown model must be null in the default item
public
Guid? Gender {
get
;
set
; }
}
public
class
MyDdlModel
{
//nullable so the default item can allow required field validation
public
Guid? MyValueField {
get
;
set
; }
public
string
MyTextField {
get
;
set
; }
}
Person person =
new
Person();
MyDdlModel ddlHint =
new
MyDdlModel { MyValueField =
null
, MyTextField =
"Gender"
};
IEnumerable<MyDdlModel> genders =
new
List<MyDdlModel>
{
new
MyDdlModel {MyTextField =
"female"
, MyValueField =
new
Guid()},
new
MyDdlModel {MyTextField =
"male"
, MyValueField =
new
Guid()},
new
MyDdlModel {MyTextField =
"other"
, MyValueField =
new
Guid()},
new
MyDdlModel {MyTextField =
"I'd rather not say"
, MyValueField =
new
Guid()}
};
void
HandleValidSubmit()
{
Console.WriteLine(
"OnValidSubmit"
);
}
}