Sharepoint Web Part validation set off by Publishing Controls

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.

Answers


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.


I went through a world of pain getting this to work so thought I'd share.

Even though I was correctly getting the display mode, my control continued to validate regardless of the mode.

The final solution involved three things:

  1. Disabling the RegularExpressionValidator by default - i.e. Enabled-"false"
  2. Moving my code into the CreateChildControls method.
  3. Adding an ELSE clause! Without it, validation was ALWAYS set to true. Don't ask me why.

And here's the code. Basic variation on previously supplied examples.

// Get the current display mode of the WPM
WebPartManager wp = WebPartManager.GetCurrentWebPartManager(Page);
String mode = wp.DisplayMode.Name;
// Enable validation in BrowseDisplayMode only
if (wp.DisplayMode == WebPartManager.BrowseDisplayMode)
{
    reqJournal.Enabled = true;
}
else
{
    reqJournal.Enabled = false;
    lblMsg.Text = "<strong>" + mode + " mode</strong>: Validation is disabled.";
}

Use the WebPartManager.DisplayMode enumeration to detect the current page mode and enable the validation only in BrowseDisplayMode

That's how the SharePoint OOTB webparts does validation.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpartmanager.displaymode.aspx


//disable javascript on sharepoint edit mode
$(function () {
    var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;

    if (inDesignMode == "1") {
        // page is in edit mode
        if ((typeof (Page_Validators) != "undefined") && (Page_Validators != null)) {
            var i;
            for (i = 0; i < Page_Validators.length; i++) {
                ValidatorEnable(Page_Validators[i], false);
            }
        }
    }
    else {
        // page is in browse mode
    } 
});

Need Your Help

JFace label provider overview

eclipse jface

Is there any good overview of the JFace label provider framework? The JFace snippets doesn't really explain the relationship between the different generations (pre-3.2, 3.3, 3.4, etc.)

Clear screen in C

c visual-studio

I am new to programming, and so I do encounter basic issues. In the program I am building, I need to clear the screen at a certain point, but it does not accept clrscr(); or system("cls"); what are...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.