Пример #1
0
        /// <summary>
        /// Validate the model
        /// </summary>
        /// <param name="context">the validation context</param>
        /// <returns></returns>
        public IEnumerable <ValidationResult> Validate(ValidationContext context)
        {
            var widgetService            = HostContainer.GetInstance <IWidgetService>();
            var localizedResourceService = HostContainer.GetInstance <IEzCMSLocalizedResourceService>();

            if (_pageTemplateService.IsPageTemplateNameExisted(Id, Name))
            {
                yield return(new ValidationResult(localizedResourceService.T("PageTemplate_Message_ExistingName"), new[] { "Name" }));
            }

            //Check if content is valid
            if (widgetService.IsPageTemplateValid(Content))
            {
                yield return(new ValidationResult(localizedResourceService.T("PageTemplate_Message_InvalidContentFormat"), new[] { "Content" }));
            }

            var razorValidMessage = string.Empty;

            try
            {
                var cacheName = Name.GetTemplateCacheName(Content);

                // Template is updated here
                EzRazorEngineHelper.TryCompileAndAddTemplate(Content, cacheName, typeof(PageRenderModel), ResolveType.Layout);
            }
            catch (TemplateParsingException exception)
            {
                razorValidMessage = exception.Message;
            }
            catch (TemplateCompilationException exception)
            {
                razorValidMessage = string.Join("\n", exception.CompilerErrors.Select(e => e.ErrorText));
            }
            catch (Exception exception)
            {
                razorValidMessage = exception.Message;
            }
            if (!string.IsNullOrEmpty(razorValidMessage))
            {
                yield return(new ValidationResult(string.Format(localizedResourceService.T("PageTemplate_Message_TemplateCompileFailure"), razorValidMessage), new[] { "Content" }));
            }
        }