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
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.
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;
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)
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
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 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.
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.
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
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)
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
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)
It does not seem to work for me either (not in the user control grapper anyway)
I'm using umbraco version 4.5.2
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.
is working on a reply...