17 August 2009

the PeopleEditor and how Validation sucks in SharePoint

Just over another hurdle in the SharePoint steeplechase. This one involves the glorious PeopleEditor control and how to make sure the user has some client-side feedback before they post their selection. Through the nest of blogs and comments about the general ASP.NET versus the universe of SharePoint problem, the best solution seems to revolve around hacking the client side javascript and doing the most manual processes imaginable to get consistent validation.

Precisely, I mean that within the PeopleEditor should support simple validation out of the gate, but the AllowEmpty property doesn't work (see Karine Bosch’s comments from this blog link). Then you end up going through the following failure steps before the aforementioned hack emerges as the winner:
  1. AllowEmpty property of PeopleEditor (Karine Bosch’s link) #FAIL
  2. ValidatorEnabled property of PeopleEditor (Karine Bosch’s link) #FAIL
  3. Adding the ASP.NET RequiredFieldValidator element within the ASP form (shereen's blog link) #FAIL
  4. Calling Page.Validate() / Page.isValid within onsubmit() (Ben Cline's blog link) #FAIL (because it is really challenging to server-side redirect /repost the user data back to the second step of an association form within a workflow association)
  5. Inserting a RequiredFieldValidator by override CreateChildControls() within the code-behind-form (see link to a blog SharePoint Solutions team, the "experts and pioneers on Microsoft SharePoint") sorry, another #FAIL, though from 2006
  6. (Now we are getting closer.. ) A similar problem with a close kin of this SPControl is solved by Greg Galipeau in his post about SharePoint DateTimeControl Validation. But since it is a long day and I'm not getting any younger with all this chasing blog trails, #FAIL
Now the winning hack, by the same person Shereen (from #3 above) who got it right the second time - thanks for being an extremely passionate ninja and posting your updated solution (which I'm going to copy below incase it disappears one day into the digital ether)
This post is an update to a previous post I had written on validating the PeopleEditor control on post back. In that particular post, I described the challenges of getting a PeopleEditor control, within a custom application page, to validate correctly as a required field on the client side (I had server side validation already in place and
working correctly).

For the most part, the solution I outlined worked well,
except for some situations where other controls on the page were performing
their own post back (for example, a data grid in edit mode). I found that on
submit of the entire form, the ASP.NET required field validator for the
PeopleEditor control would kick in informing me that the field was required.
However, there was a valid entry in the control so I could only conclude that
the post back by some other control on the page was mucking up the view state
for the PeopleEditor control and on submit the form was no longer aware that
this field did in fact have a value in it.
The MSDN article found here outlines my problem exactly with the eventual solution further down in the thread. I just wanted to highlight it here because it was exactly what I needed to resolve my issue. Thank you to Jan Lange for posting his solution!
inside the script tag, in the asp.net page's header (I've got to move to a blog that I can post code in. ridiculous blog=#FAIL)

function CheckProjectManager(source, arguments) {
if (aspnetForm.ctl00_PlaceHolderMain_ProjectManager_downlevelTextBox.value == "")
arguments.IsValid = false;
arguments.IsValid = true;

TabIndex="2" />

ClientValidationFunction="CheckProjectManager" />

Just to outline, what Jan eventually did was create a custom validator that called a javascript function to validate the control. Within that function he checks to see if the control is empty and stores the results of the validation in the IsValid property of the ServerValidateEventArgs object. Notice that he also sets the ControlToValidate property to “”.

No comments:

Post a Comment

firstly, thanks in advance for your comment - I don't get very many, so I'm sure to follow up if you leave me a way - secondly, come again.