Validating SharePoint Web Parts

A bit of perspective, a bit of a quandry, a bit of a question today.

SharePoint web parts work fine with validation controls. If you just want to display a validation like any other you would do it (using a ValidationSummary control or the validation control itself) things work fine. Just add them to the Controls property of the Web Part and render them. No worries.

Recently I had a fairly large form that contains half a dozen fields that need to be validated. Luckily they're all simple validations (required fields only). When you have a SharePoint form (using the built-in ListForm Web Part) it displays all the controls and a red "*" where required fields are. If you try to save the form without filling out any of the fields two things happen. First you get a client side message saying something like "You must specify a non-blank value for <fieldname>" (where field name is the Display Name of the field that's required). Second, it scrolls to where the control is (and if I remember correctly sets the focus to the control for entry). So here I am trying to mimic this behavior and running into some head-scratching activities.

The way I currently started my cloning process was:

  1. Create a RequiredValidator control and attach it to my control I want to validate (a TextBox) in my Web Part
  2. Create a ValidationSummary control and set the ShowMessageBox property to true and ShowSummary property to false. This will popup a client side message box that displays whatever the error message for the control is.
  3. Set the ValidationSummary.CssClass property to "hidden" to prevent it from being seen in the Web Part
  4. Render the ValidationSummary control in the RenderWebPart method

This works fine and gives you the same result as what SharePoint does with a Form, namely popup a message box and prevent you from going forward. This works great for the first control but then when you add a second control the message box shows all messages from all invalid controls. This isn't quite what I'm looking for as the SharePoint form will only give you the latest validation message and stop there. So without getting into managing all the various messages (say through a stack object) what gives? Also there must be a way to leverage the code that the Web Part framework has to do these validations and not have to write all this stuff over again (at least that's what my pea brain says to me) however there's nothing in the ClientAPI or the standard JavaScript (blech) files I can see that would help here.

Anyone got some perspectives on doing validations in Web Parts. While it may look like madness, trying to mimic what SharePoint does, there is method to it. I would prefer to have a consistent view of error messages throughout SharePoint, both stock Web Parts and custom ones, so the users don't get all peeved that our Web Parts show it one way but when working in a list or doclib they get treated differently. Or maybe I'm just kidding myself here and it's not worth the effort.

2 Comments

  • I have added a required field validator control in my child controls of my custom web part. It goes through adding the contol fine but when the page is rendered it has an error. there must be somthing that needs to be added to the page to allow the use of the web parts.

  • I had the same problem so on a whim, decided to try setting ValidationGroup for the Validators, etc on the web part to something that I knew would be unique to each web part at runtime:

    In code behind on the validator load event I set:

    RequiredFieldValidator1.ValidationGroup = Me.ClientID

    appears to work.

Comments have been disabled for this content.