Backend Custom Validation
Hey,
I have a content type with a checkbox/boolean field in the backend edit page that I want to validate. However, I want to validate it against all other db entries of the same content type to ensure that only one of them has this checkbox checked. If one of them already has this boolean true, I want the backend to refuse the submission. The closest forum post I've found to answer this question is this:
www.sitefinity.com/.../custom-validator-on-a-dynamic-content-type-field
But is this what I'm looking for? Currently in the middle of reading this bit of doc but am still uneasy. Thank you.
Hello,
Yes, you could use the IDynamicContentCreatingEvent interface as it is fired before the dynamic content is created.
First you need to subscribe to the Bootstrapper_Initialized event in the Application_Start() method in your Global.asax file and then subscribe to the IDynamicContentCreatingEvent in the Bootstrapper_Initialized handler as shown below:
using
Telerik.Sitefinity.DynamicModules.Events;
using
Telerik.Sitefinity.Services;
protected
void
Application_Start(
object
sender, EventArgs e)
Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
void
Bootstrapper_Initialized(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
if
(e.CommandName ==
"Bootstrapped"
)
EventHub.Subscribe<IDynamicContentCreatingEvent>(evt => IDynamicContentCreatingEvent(evt));
hey Dmitri, this was super helpful. and i've now got the validation in, however, as someone in the thread i posted above asked; how can I, from inside the Created/Updated events, force the DynamicContent to not be saved upon failure to validate? Thank you
Hi Bryan,
These events are exposed by Sitefinity and you can subscribe to them, but they will be executed regardless.
Doc: http://docs.sitefinity.com/for-developers-dynamic-modules-events
Another route you could explore is to: Overwriting the default LifecycleDecorator for a Content manager.
Please observe following KB-article, more specific the 2nd part;
http://www.sitefinity.com/developer-network/knowledge-base/details/overwriting-the-default-lifecycledecorator-for-a-content-manager
Additionally maybe this KB article may also be useful to you:
Validating a dynamic module field against another field
www.sitefinity.com/.../validating-a-dynamic-module-field-against-another-field
Regards,
Dimitri Cools
Telerik
Alright, final question on this topic. I've taken over the LifecycleDecorator and I can validate in it/set the selected option to false based on the validation.
Now, i accept that maybe I can't stop the publishing process from going through. But I would like to atleast alert the user that I've overridden their setting. Is there a way, from the LifecycleDecorator overriding function to alert() the browser or something? Maybe, tie into the sitefinity backend and display a message somewhere at the top of the page? The latter seems like a stretch, but I'm hoping.
I would use this:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Featured reset to false')", true);
but that requires the calling class to be a page.
Hi,
Please have a look at following forum thread also on Handling and adding a new item event in dynamic module: http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/handling-an-adding-new-item-event-in-dynamic-module
You should be able to override the ExecuteOnPublish() method and validate. In there you can throw an exception when a setting will be overwritten.
This exception will be shown as a pop-up in the back-end UI.
Regards,