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

Forums / Developing with Sitefinity / Add validation to ControlDesigner

Add validation to ControlDesigner

8 posts, 0 answered
  1. Dan
    Dan avatar
    11 posts
    Registered:
    17 Sep 2012
    23 Mar 2011
    Link to this post
    I would like to add some simple required field validation to the default control designer for a custom user control in my project.

    I created a custom control designer for my control that simply calls the base InitializeControls function, since I don't want to change the form at all.  I have also overriden the OnSaving() function, and have figured out how to check whether a given field is empty, but I can not figure out how to prevent the save from happening when one of the fields I want to require is empty.  I also can not figure out how to display a validation error message to the user.

    Does anyone have an example of how to do so?  I could not find one on the forums or in the API docs.

    Thanks, DanO
  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    28 Mar 2011
    Link to this post
    Hi Dan,

    property controls are saved by the component called PropertyEditor. In your designer, you should have such property, as it is defined on the JS interface. PropertyEditor exposes an event called "beforeSaveChanges". So, in your designer you could implement something like this:

    // ... code excluded for simplicity
     
    this._beforeSaveChangesDelegate = Function.createDelegate(this, this._beforeSaveChangesHandler);
    this.get_propertyEditor().add_beforeSaveChanges(this._beforeSaveChangesDelegate);
     
    // ... code excluded for simplicity
     
    _beforeSaveChangesHandler: function (sender, eventArgs) {
            if (this._valuesInvalid == true) {
                // cancel the saving of values
                eventArgs.set_cancel(true);
                // ... implement JS code for showing the warning messages
            }
    }

    Hope this helps.

    Best wishes,
    Ivan
    the Telerik team
  3. Dan
    Dan avatar
    11 posts
    Registered:
    17 Sep 2012
    28 Mar 2011
    Link to this post
    Ivan,

    What object is PropertyEditor a property of?  It's not a property of Telerik.Framework.Web.Design.ControlDesigner, which is the class that my custom control designer inherits from.  I'm not understanding the context of the example you provided.
     
    Thanks, Dan
  4. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    28 Mar 2011
    Link to this post
    Hi Dan,

     I wrote a very simple sample of how to implement a custom control designer in Sitefinity 4.0. It's described in this blog post:
    http://www.sitefinity.com/blogs/ivan/posts/11-01-18/implementing_sitefinity_widgets_and_designers_how_to_implement_facebook_like_button.aspx

    The PropertyEditor is a client side component, and you will have access to it in the client side component of your control designer. Sitefinity will automatically set the value of this property to the proper instance. If you examine the code in the said blog post, you will see there is a get_propertyEditor() function that you can use.

    Let me know if there is anything else I can do for you.

    Best wishes,
    Ivan
    the Telerik team
  5. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    28 Mar 2011
    Link to this post
    Hi Dan,

     I am sorry, I didn't see that the ticket is for Sitefinity 3.7... I was talking about Sitefinity 4.0. Can you attach me the code for your designer and I'll take a look if I can get the validation working.

    Once again, my apologies for misleading you.

    Best wishes,
    Ivan
    the Telerik team
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    28 Mar 2011
    Link to this post
    Hi Dan,

    There is no direct way to cancel the saving.

    One of the option is calling CloseWindow directly after you override OnSaving - before executing any other logic

    The second option is finding the ContorolEditor through recursion inside the controls collection

    var c = this.Parent.Controls[0].Page.Controls[3].Controls[3] as ControlEditor;;

    and then call Canceled event.

    Regards,
    Ivan Dimitrov
    the Telerik team
  7. Dan
    Dan avatar
    11 posts
    Registered:
    17 Sep 2012
    28 Mar 2011
    Link to this post
    Ivan,

    How did you determine where the ControlEditor was in the hierarchy?  Will those indexes work for me as well??

    And why are you using a var?  Can't you do this:
    ControlEditor c = (ControlEditor)this.Parent.Controls[0].Page.Controls[3].Controls[3];

    And how do you raise the event in this case?

    Thanks, Dan
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    31 Mar 2011
    Link to this post
    Hi Dan,

    Why shouldn't use var? I do not see what would be the problem if I use var for a sample.
    You need to use recursion to find the control. The control is inside the controls collection, because each control designer has ControlEditor.

    c.Canceled should cancel the saving. This is the only public event that you can use. Basically the control desinger does not support validation when you save the item.

    Another option is using some RadControls in the control designer that supports AJAX validation, so you can throw an alert on an earlier stage.

    Regards,
    Ivan Dimitrov
    the Telerik team
Register for webinar
8 posts, 0 answered