Validation of property with EmailAddressAttribute fails on empty value

Vote:
 

When a string property is decorated with an EmailAddressAttribute validation fails when removing the address. This occurs on a fresh Alloy site with all the latest updates.

How to reproduce:

  • Create a new Alloy site
  • Add a string property with EmailAddressAttribute to a content type, I used ContactBlock and added:
    • [Display(Order = 30), EmailAddress]
    • public virtual string Email { get; set; }
  • Create a block in the CMS, set an email address, and publish
  • Edit the block and erase the email address

Here validation fails with: The Email field is not a valid e-mail address.

Sent via Dojo to the server in the post body is: {"id":"113_116","properties":{"email":"\"\""},"action":513}

As an editor this can be worked around by leaving the property blank, switching to another property and changing that after which validation succeeds and nothing is sent for the property.

Is this a known bug? Is there a fix available?

#202191
Mar 19, 2019 10:52
Vote:
 

Hi Mattias,

Not seen this before, but it looks like it's because the string is empty. If you look at the IsValid method for the EmailAddressAttribute you see it returns true if null (but doesn't have any special handling for empty strings):

if (value == null) {
    return true;
}

I think the easiest fix would be to lift the EmailAddressAttribute from Alloy and add in a string.IsNullOrEmpty(), then use that custom email attribute.

/Jake

#202208
Mar 19, 2019 15:34
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.