コード例 #1
0
        /// <summary>
        /// Validate the model
        /// </summary>
        /// <param name="context">the validation context</param>
        /// <returns></returns>
        public IEnumerable <ValidationResult> Validate(ValidationContext context)
        {
            var templateService          = HostContainer.GetInstance <IWidgetTemplateService>();
            var localizedResourceService = HostContainer.GetInstance <IEzCMSLocalizedResourceService>();

            if (templateService.IsTemplateNameExisted(Id, Name))
            {
                yield return(new ValidationResult(localizedResourceService.T("WidgetTemplate_Message_ExistingName"), new[] { "Name" }));
            }

            var type = Type.GetType(DataType);

            if (type != null)
            {
                var razorValidMessage = EzRazorEngineHelper.ValidateTemplate(Name, FullContent, type);
                if (!string.IsNullOrEmpty(razorValidMessage))
                {
                    yield return(new ValidationResult(string.Format(localizedResourceService.T("WidgetTemplate_Message_FullContentCompileFailure"), razorValidMessage), new[] { "FullContent" }));
                }

                //Generate content from widgets
                var content = WidgetHelper.GetFullTemplate(Content, string.Empty, string.Empty, DataType, Shortcuts);
                razorValidMessage = EzRazorEngineHelper.ValidateTemplate(Name, content, type);
                if (!string.IsNullOrEmpty(razorValidMessage))
                {
                    yield return(new ValidationResult(string.Format(localizedResourceService.T("WidgetTemplate_Message_ContentCompileFailure"), razorValidMessage), new[] { "Content" }));
                }

                razorValidMessage = EzRazorEngineHelper.ValidateTemplate(Name, Script, type);
                if (!string.IsNullOrEmpty(razorValidMessage))
                {
                    yield return(new ValidationResult(string.Format(localizedResourceService.T("WidgetTemplate_Message_ScriptCompileFailure"), razorValidMessage), new[] { "Script" }));
                }

                razorValidMessage = EzRazorEngineHelper.ValidateTemplate(Name, Style, type);
                if (!string.IsNullOrEmpty(razorValidMessage))
                {
                    yield return(new ValidationResult(string.Format(localizedResourceService.T("WidgetTemplate_Message_StyleCompileFailure"), razorValidMessage), new[] { "Style" }));
                }

                if (Shortcuts != null)
                {
                    var widgetValidationMessages = new Dictionary <string, string>();
                    foreach (var widget in Shortcuts)
                    {
                        razorValidMessage = EzRazorEngineHelper.ValidateTemplate(widget.Name, widget.Content, type);
                        if (!string.IsNullOrEmpty(razorValidMessage))
                        {
                            widgetValidationMessages.Add(widget.Name, razorValidMessage);
                        }
                    }

                    if (widgetValidationMessages.Any())
                    {
                        var message = string.Join(FrameworkConstants.BreakLine, widgetValidationMessages.Select(widget => localizedResourceService.TFormat("WidgetTemplate_Message_WidgetCompileFailure", widget.Key, widget.Value)));
                        yield return(new ValidationResult(message, new[] { "Widgets" }));
                    }
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Validate template
 /// </summary>
 /// <param name="name"></param>
 /// <param name="content"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static string ValidateTemplate(string name, string content, Type type)
 {
     return(EzRazorEngineHelper.ValidateTemplate(name, content, type));
 }