Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • greengiant83 88 posts 109 karma points
    May 12, 2011 @ 19:47
    greengiant83
    0

    Validating Custom Datatypes

    What is the best practice for validating custom datatypes (a IUsercontrolDataEditor)?  In general it looks like umbraco will defer validation for an document until it attempts to be published, which seems cool because it lets you work on a document without having to finish it.  How can validate my usercontrol's data at publish while allowing it be saved in an incomplete/invalid state?

  • Lennart Stoop 304 posts 842 karma points
    May 12, 2011 @ 20:05
    Lennart Stoop
    0

    Hi,

    As of Umbraco 4.6 you can simply add a validation property to your custom data editor:

        [ValidationProperty("IsValid")]
        public partial class MyDataEditor : System.Web.UI.UserControl,
            umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor

    After you have added the attribute, you can implement the property by adding your validation logic. Umbraco will call it during publish:

            public string IsValid
            {
                get
                {
                    if (CheckIfDataIsValid())
                        return "Valid";
                    else
                        return String.Empty;
                }
            }

     

    Hope this helps :-)

     

  • greengiant83 88 posts 109 karma points
    May 12, 2011 @ 20:17
    greengiant83
    0

    Lennart,

    This sounds like it is exactly what i am looking for.  However, it does not seem to have any impact.  I am using umbraco v 4.6.1 (Assembly version: 1.0.4029.25836).  When I publish a document with this datatype on it. It publishes just fine.  Shouldnt it present an error to the user saying something about the data being invalid?

        [ValidationProperty("IsValid")]
        public partial class ProcessingFeeEditor : System.Web.UI.UserControl, IUsercontrolDataEditor
        {
            public string IsValid
            {
                get
                {
                    if (CheckIfDataIsValid())
                        return "Valid";
                    else
                        return String.Empty;
                }
            }
            private bool CheckIfDataIsValid()
            {
                return false;
            }

  • Lennart Stoop 304 posts 842 karma points
    May 12, 2011 @ 20:25
    Lennart Stoop
    0

    I forgot to mention: you have to make the document's property a mandatory one for the validation to fire. Or did you?

  • greengiant83 88 posts 109 karma points
    May 12, 2011 @ 20:29
    greengiant83
    0

    I hadnt, but I just changed that and tested again, and it still does not display any errors and allows the publish to happen.  I put a breakpoint on the IsValid property, and it never gets accessed during the publish

  • Lennart Stoop 304 posts 842 karma points
    May 12, 2011 @ 20:39
    Lennart Stoop
    0

    I looked up an old thread because I experienced the same issue not too long ago, and looks like I still haven't learned lol:

    http://our.umbraco.org/forum/developers/extending-umbraco/14157-Make-custom-datatype-mandatory?p=2

    So it's actually the value property that is evaluated by Umbraco. You just have to make sure you return null or an empty string if your data is invalid.

    For example:

            public object value
            {
                get
                {
                    if (CheckIfDataIsValid())
                        return myData;
                    else
                        return null;
                }
            }

    It's not ideal though, as it seems you cannot validate partial data.

  • greengiant83 88 posts 109 karma points
    May 12, 2011 @ 20:58
    greengiant83
    0

    I figured out your original suggestion.  Specifying the ValidationProperty allows umbraco to pull that value and compare it against the validation regex in the document properties.  This means for your example to work, you just need to drop the text "Valid" into the validation regex field in the document properties.  It is a little lackluster because I dont see a way to provide a detailed error message back to the user about what the problem is, but it is stilll a viable solution.

  • Lennart Stoop 304 posts 842 karma points
    May 12, 2011 @ 21:08
    Lennart Stoop
    0

    Oh I didn't realize the validation property is associated to the regex validation, that's good to know.

    Using the value property as mandatory validation does work though, its just not that granular when working with complex values and as you say there's not really an option to provide detailed information on what's missing.

  • greengiant83 88 posts 109 karma points
    May 12, 2011 @ 23:19
    greengiant83
    0

    I was mistaken.  I thought that the regex validation would apply to the property specified by the ValidationProperty attribute.  I have been unable to prove that is the case.  Umbraco seems to ignore this attribute and just look at the value object.  This is still a workable method for validating, though, it is a little less elegant if you are storing non trivial values.

  • greengiant83 88 posts 109 karma points
    May 13, 2011 @ 22:59
    greengiant83
    0

    Ok, we were over complicating this. It looks like umbraco utilizes the standard .net validation framework, so you can use the standard .net validator controls.  You just have to set EnableClientScript to false.

Please Sign in or register to post replies

Write your reply to:

Draft