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?
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;
}
}
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; }
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
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.
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.
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.
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.
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.
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?
Hi,
As of Umbraco 4.6 you can simply add a validation property to your custom data editor:
After you have added the attribute, you can implement the property by adding your validation logic. Umbraco will call it during publish:
Hope this helps :-)
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?
I forgot to mention: you have to make the document's property a mandatory one for the validation to fire. Or did you?
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
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:
It's not ideal though, as it seems you cannot validate partial data.
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.
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.
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.
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.
is working on a reply...