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

Forums / Developing with Sitefinity / Backend Custom Validation

Backend Custom Validation

6 posts, 3 answered
  1. bryan
    bryan avatar
    10 posts
    Registered:
    11 Apr 2016
    28 Apr
    Link to this post

    Hey,

    I have a content type with a checkbox/boolean field in the backend edit page that I want to validate. However, I want to validate it against all other db entries of the same content type to ensure that only one of them has this checkbox checked. If one of them already has this boolean true, I want the backend to refuse the submission. The closest forum post I've found to answer this question is this:

    http://www.sitefinity.com/developer-network/forums/general-discussions-/custom-validator-on-a-dynamic-content-type-field

     

    But is this what I'm looking for? Currently in the middle of reading this bit of doc but am still uneasy. Thank you.

    Answered
  2. Dimitri Cools
    Dimitri Cools avatar
    80 posts
    Registered:
    08 Dec 2016
    29 Apr
    Link to this post
    Hello,

    Yes, you could use the IDynamicContentCreatingEvent interface as it is fired before the dynamic content is created.

    First you need to subscribe to the Bootstrapper_Initialized event in the Application_Start() method in your Global.asax file and then subscribe to the IDynamicContentCreatingEvent in the Bootstrapper_Initialized handler as shown below:

    using Telerik.Sitefinity.DynamicModules.Events;
     
    using Telerik.Sitefinity.Services;
     
        
     
    protected void Application_Start(object sender, EventArgs e)
     
    {
     
    Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
     
    }
     
        
     
    void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
     
    {
     
    if (e.CommandName == "Bootstrapped")
     
    {
     
    EventHub.Subscribe<IDynamicContentCreatingEvent>(evt => IDynamicContentCreatingEvent(evt));
     
    }
     
    }



    Then you can implement the IDynamicContentCreatingEvent method where you add your logic.
     

    Regards,
    Dimitri Cools
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. bryan
    bryan avatar
    10 posts
    Registered:
    11 Apr 2016
    29 Apr
    Link to this post

    hey Dmitri, this was super helpful. and i've now got the validation in, however, as someone in the thread i posted above asked; how can I, from inside the Created/Updated events, force the DynamicContent to not be saved upon failure to validate? Thank you

    Answered
  4. Dimitri Cools
    Dimitri Cools avatar
    80 posts
    Registered:
    08 Dec 2016
    02 May
    Link to this post
    Hi Bryan,

    These events are exposed by Sitefinity and you can subscribe to them, but they will be executed regardless.

    Doc: http://docs.sitefinity.com/for-developers-dynamic-modules-events

    Another route you could explore is to: Overwriting the default LifecycleDecorator for a Content manager.
    Please observe following KB-article, more specific the 2nd part;
    http://www.sitefinity.com/developer-network/knowledge-base/details/overwriting-the-default-lifecycledecorator-for-a-content-manager

     Additionally maybe this KB article may also be useful to you:
    Validating a dynamic module field against another field
    http://www.sitefinity.com/developer-network/knowledge-base/details/validating-a-dynamic-module-field-against-another-field

    Regards,
    Dimitri Cools
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  5. bryan
    bryan avatar
    10 posts
    Registered:
    11 Apr 2016
    02 May
    Link to this post

    Alright, final question on this topic. I've taken over the LifecycleDecorator and I can validate in it/set the selected option to false based on the validation. 

    Now, i accept that maybe I can't stop the publishing process from going through. But I would like to atleast alert the user that I've overridden their setting. Is there a way, from the LifecycleDecorator overriding function to alert() the browser or something? Maybe, tie into the sitefinity backend and display a message somewhere at the top of the page? The latter seems like a stretch, but I'm hoping.

     

    I would use this:

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Featured reset to false')", true);

    but that requires the calling class to be a page. 

    Answered
  6. Dimitri Cools
    Dimitri Cools avatar
    80 posts
    Registered:
    08 Dec 2016
    04 May
    Link to this post
    Hi,
    Please have a look at following forum thread also on Handling and adding a new item event in dynamic module: http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/handling-an-adding-new-item-event-in-dynamic-module

    You should be able to override the ExecuteOnPublish() method and validate. In there you can throw an exception when a setting will be overwritten. 
    This exception will be shown as a pop-up in the back-end UI.

    Regards,

    Dimitri Cools
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
6 posts, 3 answered