Copied to clipboard

Flag this post as spam?

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


  • James Crane 34 posts 80 karma points
    Sep 03, 2009 @ 22:53
    James Crane
    0

    Making a bespoke data type mandatory

    Hi,

    I raised this in a previous post a while back noone was able to answer my question, and I had no success myself.

     

    I have built a datatype that is a dropdown list populated from a set of nodes.The problem is I cannot get Umbraco to make it mandatory as no matter what I post back it just lets it go. The code is:

     

    public partial class usercontrols_DTProductSharedDescription : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor  

    {

        public string umbracoValue;

     

        protected void Page_Load(object sender, EventArgs e)

        {

            if (Page.IsPostBack)

            {

                umbracoValue = uxSharedDescriptions.SelectedItem.Value; 

            }

            else

            {

                Node sharedDescription;

                ListItem selectedItem;

                ListItem defaultItem;

     

                sharedDescription = new Node(1084);

     

                uxSharedDescriptions.DataSource = sharedDescription.ChildrenAsTable();

                uxSharedDescriptions.DataValueField = "Id";

                uxSharedDescriptions.DataTextField = "NodeName";

                uxSharedDescriptions.DataBind();

     

                defaultItem = new ListItem("Please select...", "-1");

                uxSharedDescriptions.Items.Insert(0, defaultItem);

     

                selectedItem = (ListItem)uxSharedDescriptions.Items.FindByValue(umbracoValue);

                if (selectedItem != null)

                {

                    selectedItem.Selected = true;

                }

     

            }

        }

     

        public object value

        {

            get

            {

                return umbracoValue;

            }

            set

            {

                umbracoValue = value.ToString();

            }

        }

    }

     

    I'm really struggling knowing to make this validate properly when it's selected as mandatory in the Umbraco client.

     

    Setting the return value to 0 does not work, nor does making it null.

     

    Many thanks

    JC

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Sep 03, 2009 @ 23:31
    Peter Gregory
    3

    Hi James

    To validate a datatype control in umbraco you need to do the following.  Above the class declaration in the codebehind do this

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

    ValidationProperty Defines the metadata attribute that ASP.NET server controls use to identify a validation property.

    somewhere in your codebehind you will need to create a public string property that will will return a string if the control is valid.

    public string IsValid
            {
                get
                {
                    var valid = "Valid";
                    if( .... your test failure check or whatever ...){
    valid = "";
                    }
                    return valid;
                }
            }

    This should sort you out.  I have never tried this with the UserControl Wrapper but I assume its the same as rolling your own Datatype from scratch as the wrapper inherits from the same stuff from what I can tell.

    Hope that works.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 04, 2009 @ 03:57
    Aaron Powell
    0

    Pete - I think I found something for you to do in a blog post ;)

    I had no idea of this and was asked the other day about it, I shrugged and said I didn't know how to do it :P

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Sep 04, 2009 @ 10:21
    Niels Hartvig
    0

    The cool thing is that there's no difference between doing this with a datatype and an ordinary .NET custom control. Nice, huh :-)

    (not sure it'll work with the user control wrapper though)

  • James Crane 34 posts 80 karma points
    Sep 04, 2009 @ 11:05
    James Crane
    0

    Hi Peter,

    Thanks so much for your help here. I have to admit though I'm still struggling to make this work.

    My code is now:

     

    [ValidationProperty("IsValid")]
    public partial class usercontrols_DTProductSharedDescription : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor 
    {
        public string umbracoValue;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                umbracoValue = uxSharedDescriptions.SelectedItem.Value;
            }
            else
            {
                Node sharedDescription;
                ListItem selectedItem;
                ListItem defaultItem;

                sharedDescription = new Node(1084);

                uxSharedDescriptions.DataSource = sharedDescription.ChildrenAsTable();
                uxSharedDescriptions.DataValueField = "Id";
                uxSharedDescriptions.DataTextField = "NodeName";
                uxSharedDescriptions.DataBind();

                defaultItem = new ListItem("Please select...", "-1");
                uxSharedDescriptions.Items.Insert(0, defaultItem);

                selectedItem = (ListItem)uxSharedDescriptions.Items.FindByValue(umbracoValue);
                if (selectedItem != null)
                {
                    selectedItem.Selected = true;
                }

            }
        }

        public object value
        {
            get
            {
                return umbracoValue;
            }
            set
            {
                umbracoValue = value.ToString();
            }
        }
        public string IsValid
            {
                get
                {
                    string valid = "Valid";
                    if(uxSharedDescriptions.SelectedItem.Value == "-1")
                    {
                          valid = "";
                    }
                    return valid;
                }
            }

    }

    But this IsValid method isn't being called by Umbraco, so isn't working.

     

    I'm still very new to Umbraco so please do bear with me.

     

    Many thanks

    JC

  • Webious 22 posts 26 karma points
    Feb 23, 2010 @ 20:14
    Webious
    0

    I have run into the same problem with ubmraco 4.0.2.1 (don't ask why I haven't upgraded, 4.0.3 didn't work out for me).Everything is applied as Peter suggested:

     

    ...

    [ValidationProperty("IsValid")]

        public partial class CustomDataType : System.Web.UI.UserControl, IUsercontrolDataEditor

        {

            public string umbracoValue;

    public string IsValid  { get{return (testsPassed)?"valid":string.Empty;} }

    ...

     

    but the method never gets reached, I even tried throwing an exception inside the getter, but the the code just does not execute (I HAVE set the required checkbox on document property)

     

  • Murray Roke 503 posts 966 karma points c-trib
    Dec 21, 2010 @ 22:12
    Murray Roke
    0

    It does not seem to work for me either (not in the user control grapper anyway)

    I'm using umbraco version 4.5.2

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 21, 2010 @ 22:16
    Tom Fulton
    0

    Did you see this more recent post on the subject?  http://our.umbraco.org/forum/developers/extending-umbraco/14157-Make-custom-datatype-mandatory?p=0

    Jeroen has posted some workarounds, not sure if they work with the usercontrolgrapper though.

    But this has been fixed for 4.6.

Please Sign in or register to post replies

Write your reply to:

Draft