+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom Validation for Extended Templates

Custom Validation for Extended Templates

5 posts, 1 answered
  1. William
    William avatar
    108 posts
    Registered:
    15 Feb 2011
    04 Nov 2011
    Link to this post
    Hey Telerik and forum folks,

    I'm working on a project for 3.7 and am running into a need to add custom fields to the News module.  I've read the info on extending those templates and have successfully done so. 

    Now I'm needing to apply some custom validation to those templates.  The actual custom validation is a bit complicated to explain, so I'll give you an example.  Let's say a user enters a date for some news item.  I want to provide validation that is triggered if more than 5 news items are set to run on that same day.

    What I'd need for that in the user control world is a CustomValidator that does a quick lookup on the server and validates the control.  It can't be done on the client side. 

    However, within the template I can't seem to make that happen. 

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
        }
     
        protected void TestCV_ServerValidate(object source, ServerValidateEventArgs args)
        {
               args.IsValid = false;
           
    TestCV.IsValid = false;
        }
         
    </script>

    <asp:CustomValidator ID="TestCV" runat="server" ValidationGroup="InsertNews"
        EnableClientScript="false" Display="Dynamic" ForeColor="" CssClass="validMessage"
        OnServerValidate="TestCV_ServerValidate">Invalid
        </asp:CustomValidator>

    By doing some debugging work, I find that on submit, Page.IsValid is false.  And yet, the page is still submitting and still adding the news item. 

    Is there something more I need to do to intercept the submit to prevent it from happening?  It seems like it's ignoring Page.IsValid and going on it's way without heeding the validation control.

    Thanks for any help you can give.

    - William
  2. William
    William avatar
    108 posts
    Registered:
    15 Feb 2011
    04 Nov 2011
    Link to this post
    Here's a better example of testing this:

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Page.Validate("InsertNews");
                Response.Redirect(Page.IsValid.ToString());
            }
        }
     
        protected void TestCV_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;
        }
     
    </script>

    Obvious here that even though the Page is not valid, it is still saving the content.  I'm hoping what I'm missing is simple, but I assuming it's probably something fairly complicated.

    Thanks

    - William
  3. William
    William avatar
    108 posts
    Registered:
    15 Feb 2011
    08 Nov 2011
    Link to this post
    Anyone?
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    09 Nov 2011
    Link to this post
    Hello William,

    In order to be able to use the custom validator you need to check if the page is valid in the save event of the news item insert/edit views. The easiest way to achieve this is if you replace the built in view with a custom one as described here. Here is what you have to do for inserting news items.

    1) Leave the validation method just as you have it. It will execute its logic for validating the item.
    2) In App_Code directory create a custom class which inherits from NewsItemNew:
    using Telerik.News.WebControls.Admin;
     
    /// <summary>
    /// Summary description for CustomNewsItemNew
    /// </summary>
    public class CustomNewsItemNew: NewsItemNew
    {
        protected override void SaveContent()
        {
            //check if page is valid
            if (Page.IsValid)
                base.SaveContent();
        }
        //set path to the external template
        public override string LayoutTemplatePath
        {
            get
            {
                return "~/Sitefinity/Admin/ControlTemplates/News/NewsItemNew.ascx";
            }
            set
            {
                base.LayoutTemplatePath = value;
            }
        }
        public override System.Type LocalizationAssemblyInfo
        {
            get
            {
                return typeof(NewsItemNew);
            }
            set
            {
                base.LocalizationAssemblyInfo = value;
            }
        }
     
        public override System.Type AssemblyInfo
        {
            get
            {
                return typeof(NewsItemNew);
            }
            set
            {
                base.AssemblyInfo = value;
            }
        }
     
    }

    In the controls config file instead of mapping a template you must map the custom niew (remove the template mappings for inserting news views. The controls config file should have something as bellow:
    <?xml version="1.0" encoding="utf-8" ?>
    <controlsConfig>
      <viewMap>
          <viewSettings hostType="Telerik.News.WebControls.Admin.NewsItemsView, Telerik.News">
              <views>
                  <remove name="NewsItemNew"></remove>
                  <add name="NewsItemNew" viewType="CustomNewsItemNew"></add>
              </views>
          </viewSettings>
      </viewMap>
    </controlsConfig>

    Now when you set the page to invalid in your custom validation method save will not occur.

    Regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  5. William
    William avatar
    108 posts
    Registered:
    15 Feb 2011
    09 Nov 2011
    Link to this post
    Radoslav,

    That worked perfectly, and very easily too.  Thank you SO much.

    - William
Register for webinar
5 posts, 1 answered