I have a web part which uses validation (InputFormRequiredFieldValidator
) on a textbox field to prevent submission of an empty field. When clicking on Check In to Share Draft or Publish while in Edit mode, this validation is done, and since I am not actually trying to submit the form, but rather check it in, I'd rather this didn't happen.
How can I achieve this?
See also: Sharepoint web part form validation blocks updating web part settings - this has the validation code, and how I solved the problem of the EditorPart setting off the validation.
Update: I've tried detecting EditDisplayMode and disabling the validator as follows:
if (WebPartManager.DisplayMode.Equals(WebPartManager.EditDisplayMode))
{
messageRequiredValidator.Enabled = false;
}
This doesn't work - I still get the validation failure message when Checking in the page. Maybe I am not detecting the DisplayMode correctly.
As Madhur pointed out I needed to check for BrowseDisplayMode. I started to go down the route of switching off the validator when in Edit mode and relying on the default of true, but there are other display modes such as Design, where I might also get the problem. So I checked for BrowseDisplayMode as follows:
WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(Page);
if (mgr.DisplayMode.Equals(mgr.SupportedDisplayModes["Browse"]))
{
messageRequiredValidator.Enabled = true;
}
else
{
messageRequiredValidator.Enabled = false;
}
This seems to do the trick. Would appreciate any feedback anyone has on this method.