1

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

rspears69
  • 435
  • 1
  • 7
  • 19

1 Answers1

3

Here's an alternative I can suggest:

public string Url 
{ 
   get { return _value; } 
   set { _value = string.IsNullOrWhiteSpace(value) ? null : value; }
} 

original which doesn't seem to work as advertised:

You need to add AllowEmptyStrings to your Required annotation and include it. I.e.,

[Required(AllowEmptyStrings = true)]
[DisplayFormat(ConvertEmptyStringToNull = false)]

See this question/answer: Allow empty strings for fields marked with PhoneAttribute or UrlAttribute

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
user15716642
  • 171
  • 1
  • 12
  • Thanks, but please read my question more carefully. I don't want it to be `[Required]`, and I DO want empty strings converted to null. – rspears69 Nov 29 '21 at 17:37
  • I did read your question. I just thought you didn't want it to be required because you thought required wouldn't allow for empty strings. My apologies. – user15716642 Nov 29 '21 at 17:41
  • 1
    Are you opposed to doing something like this? public string Url { get { return _value; } set { _value = string.IsNullOrWhiteSpace(value) ? null : value; } } – user15716642 Nov 29 '21 at 17:44
  • Well to be honest, if this would allow the behavior I want, I would use it, but it doesn't. I still get a "Link field is required" validation message when I try to create an object. I've also tried using this and setting the Link field to an empty string in the code, but the validation tells me "The Link field is not a valid fully-qualified http, https, or ftp URL." which seems to go against the idea of "allowing empty strings". – rspears69 Nov 29 '21 at 17:46
  • 3
    I am not opposed to that, and I forgot to try it. It seems to work! – rspears69 Nov 29 '21 at 17:48
  • Worked for me! Thanks! – remixtedi Jul 20 '23 at 11:45