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?

#205043
Jun 27, 2019 6:43
Vote:
 

Hi Joseph,

I don't know that this is known bug or not but you can create your custom Validation attribute  and use that, instead of this.

public class CustomEmailAddressAttribute : DataTypeAttribute
    {
        private static readonly Regex ValidationRegex = new Regex(@"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$", RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
        
        public EmailAddressAttribute() : base(DataType.EmailAddress)
        {
        }

        public override bool IsValid(object value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return true;
            }
            return ((value is string input) && (ValidationRegex.Match(input).Length > 0));
        }
}

Thanks

Ravindra

#205049
Jun 27, 2019 9:15
Vote:
 
#205052
Jun 27, 2019 10:19
* 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.