November Happy Hour will be moved to Thursday December 5th.

Linus Ekström
Dec 4, 2013
  6893
(2 votes)

Email validation attribute and support for IMetadataAware

In this blog post I will describe one new feature in EPiServer 7.5 as well as another feature that was actually removed in a last minute change (as well as describing how you can implement this yourself). The feature that was removed was an EPiServer attribute called EmailAttribute that could be applied to a string property to enable email validation. In code it would look like this:

[Email]
public virtual string Email { get; set; }

 

The reason for us to remove this was that there actually exists such a attribute in the System.ComponentModel.DataAnnotations namespace in .NET 4.5 (well, actually it’s called EmailAddressAttribute so there was a slight naming difference). So to avoid confision, we have removed our attribute. Using the .NET attribute should work with the limitation that the validation will be done server side since we have not added logic to send settings to the client yet. If you want to use an attribute that has support for client side validation as well as the ability to add several comma-separated email addresses you can add your own attribute with the following code:

using System;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using EPiServer.Framework.Localization;
 
namespace Samples
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class EmailAttribute : ValidationAttribute, IMetadataAware
    {
        public void OnMetadataCreated(ModelMetadata metadata)
        {
            var extendedMetadata = metadata as ExtendedMetadata;
 
            if (extendedMetadata == null)
            {
                return;
            }
 
            string emailRegexPattern = AllowMultiple ? Framework.Validator.MultipleEmailRegexString : Framework.Validator.EmailRegexString;
 
            extendedMetadata.EditorConfiguration["pattern"] = emailRegexPattern;
            extendedMetadata.EditorConfiguration["invalidMessage"] = LocalizationService.Current.GetString("/episerver/shell/ui/resources/JQueryValidate/email");
        }
 
        public override string FormatErrorMessage(string name)
        {
 
            return LocalizationService.Current.GetString("/episerver/shell/ui/resources/JQueryValidate/email");
        }
 
        public override bool IsValid(object value)
        {
            var stringValue = value as string;
            if (String.IsNullOrEmpty(stringValue))
            {
                return true;
            }
 
            if (AllowMultiple)
            {
                return Framework.Validator.MultipleEmailRegex.IsMatch(stringValue);
            }
            else
            {
                return Framework.Validator.EmailRegex.IsMatch(stringValue);
            }
        }
 
        public bool AllowMultiple { get; set; }
    }
}

IMetadataAware

If you take a look at the class above, the class inherrits from ValidationAttribute and there are two methods that are overridden to implement server side validation: IsValid and FormatErrorMessage. This was possible in the EPiServer 7 release so nothing new here. What’s new, however, is the support in EPiServer for the IMetadataAware interface (from System.Web.Mvc). I’m very excited about this since it makes it possible to add attributes that can handle both client and server side validation in one class which should bring on possibilities to add lots of cool validation attributes. In the class above, we define the “pattern” setting which is used by the standard text editor to validate input against a regular expression.

Note that the class above is using a translation from the EPiServer resources files directly. This is not part of our supported API and might change without notice over time…

Dec 04, 2013

Comments

Petter Klang
Petter Klang Dec 19, 2013 12:03 PM

Thanks for sharing this. Will surely come in handy

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog