WebPart: validate properties and prevent saving

Sergiu Filip asked on March 2, 2017 19:13

Hi everyone ! (this is a Kentico 8 question): when in 'Design' mode configuring a custom web-part (derives from CMSAbastractWebpart), how (where) can I validate if the configuration values are complete and aligned with business rules and, when failed - display an error message and prevent the saving ?

Recent Answers


Trevor Fayas answered on March 2, 2017 19:23

Fields have a "Required" checkbox which means there has to have a value, and then aside from that there is "Validation" rules you can apply on each field.

Some catches though:

  • If a field is not set to required, they can put nothing.
  • If the field is blank, then NO validation rule will be ran, so if you want to gaurentee the input matches a rule, you have to make it required so there is a value.
  • Additionally, if a field's Visibility is set to False (and i think Enabled) then no validation is ran, and the required is not required then.

Hope that helps!

1 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 2, 2017 19:30 (last edited on March 2, 2017 19:33)

You normally validate your business rules in the the form tab, the design tab is for development. You can define some rules/restriction for your document type and if you need to go further you can use global events, i.e. when an editor saves a document you can implement your custom validation logic. On top of that you have workflows. Design tab is for development, our editors don't event know that it exists.

0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 2, 2017 19:32

Hi Trevor ! thank you for the post; yes, those are obvious and I am aware of the UI features, but that only allows for very limited validation rules; I need to do something more sophisticated in the code behind (sorry for not being clear on that).

Cheers 1 Sergiu

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 2, 2017 19:35

Ah, well i still always promote doing things through the UI. Have you first looked into creating your own Macro Rules in Kentico? That is where you can create even more advanced logic, passing multiple values in, etc.

Along with that, you can create Custom Macros that can handle lookups using the back end API. Build the tools you need in the back end, so you can manage in the front end, best way to go about it!

1 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 2, 2017 20:01

Hi Peter,Trevor, thank you for the posts; well... I think I fail to make myself clear: - I am a C# dev and I develop a relatively complex web-part (which eventually will be exposed to WEB authors as a widget). - Say I have a bunch (2 dozen) configuration properties for the web part, and only certain combinations of values render the web-part functional; say - if Prop_A is this, and Prop_M is that, Prop_Z has only two possible values. I want to implement that logic: if valid allow the save ! If not, decline & show a warning. Very easy do be done in code behind, right ?

 That addition of the web-part to the page MUST be done in the 'Design' tab, right Peter ?

Thanks, Sergiu

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 2, 2017 20:10

Aye, that seems like a lot of logic.

There is an overridable method "IsValid()" that you should implement, i believe this is run once the properties are available and you should be able to put the logic in there and return false if it isn't valid, i think you can set the error somewhere too....

Not 100% if this is run on WebPart Configuration (so if they try to save the configuration it checks this), or if it's run on web part rendering (in which case they can save a wrongly configured webpart but it won't display). That you'll need to check!

 public override bool IsValid()
    {
        return base.IsValid();
    }
0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 2, 2017 20:53

Yes, but that is only part of CMSAbstractEditableWebPart which is designed to accept input and save that with the document (which makes sense): you attempt to validate the content that will be actually forged into the content - I think. So, I can "stretch" my needs and inherit from that bu it will be a hack - this web part only spits-out markup.

Thanks, S.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 3, 2017 14:29

You are correct, the IsValid() method is not available in the CMSAbstractWebPart class. You have a few options:

  • create your own method and do your checking within there.
  • validate your public properties upon being set or retrieved.
  • create your own custom form controls for each of these fields which you can then utilize the IsValid() method and simply assign those controls in the UI and make your webpart simpler.
0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 3, 2017 15:30

Apologies on not seeing the IsValid is only part of the editableWebPart, the webpart i opened just happened to be the editable text one, didn't check the namespace!

0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 3, 2017 16:39

Thank you all for chiming in; yup, I think all this will work but - again, and this points back to the poor level of documentation when it comes to K. - the key aspect here is "prevent the form to save any changes" to the web part if validation fails (well, I guess I can dig a bit in the core code to understand what I can hook up into).

Big thanks ! Sergiu

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 3, 2017 16:59

The documentation cannot possibly cover all scenarios. For validation, this is documented very well for any form control. You're doing custom development. You can do your validation any way you please, so it's unfair to "blame" Kentico for poor documentation when you're really unsure of the tools available to you.

As I stated, there are options for you to do what you need/want. Just because there isn't anything out of the box or within the API specifically for your needs doesn't mean it can't be done or the product is poor. If you wish to have something evaluated to be added, then tell Kentico at ideas.kentico.com.

Lastly, contact Kentico Support and they may be able to provide you with a solution.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 3, 2017 17:32

I would like to second Brenden's comments. That's one of the things i love most about Kentico, it's extensive documentation. Anything not covered in that usually can be answered by Devnet or a quick email to Support, i've utilized support many times and usually get a resolution within the same day!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.