I have a Blazor EditForm
that I'm using to edit a previously created object. One of the fields the user is allowed to edit is a string
property called Link
which is annotated with [Url]
. This field is not annotated with the [Required]
attribute, and I don't want it to be.
When creating the object, I am able to leave this field blank in the form, no problem. If I edit an object with an already blank Link
field, it works fine as well.
The problem arises when I try to edit an object, and delete a Link
that was previously in the object (clearing the form field). I get the Url DataAnnotation message ("The Link field is not a valid fully-qualified http, https, or ftp URL.") and I'm unable to submit the form. I added another DataAnnotation to the property, [DataFormat(ConvertEmptyStringToNull = true)]
, thinking this would solve my problem and convert the empty string to a null, but it doesn't change anything.
Property:
[Url]
[DisplayFormat(ConvertEmptyStringToNull = true)]
public string Link { get; set; }
Form:
<div class="form-group">
<label for="Link">Link</label>
<InputText @bind-Value=Item.Link class="form-control"/>
<ValidationMessage For=@(() => Item.Link)/>
</div>
Edit: This seems to be "by design" (i.e. it's a bug that the .NET engineers refuse to fix) https://github.com/dotnet/runtime/issues/53820