Today I was faced with one of the known “partner-opportunities” (or bugs as you know them) from SharePoint.
Let ‘s say you have a list where 1 or more fields are mandatory. (with the red asterisk).
When you add an attachment and hit the save button, while a mandatory field is not filled in, the page will reload with error messages. The attachments however, are gone!
It was already there in SP2007, it is still here in SP2010.
So, what is a SharePoint guy (or girl! Don’t want to leave anyone in the cold here) to do ?
That solution is ,once again, called jQuery. I already blogged about the PreSaveAction() and the powers it holds regarding validation.
Well, if you use the PreSaveAction() to check on mandatory fields instead of the default SharePoint-way, you will not lose the attachments.
The following code will check if the field “Total Cost” was filled in. If not, it will show a messagebox and hold the save.
<script src="LINK TO YOUR JQUERY FILE" type="text/javascript"> function PreSaveAction() { var txtcost = $(":input[title='Total Cost']").val(); if(txtcost =="") { alert("Please fill in a value"); return false; } return true; }; </script>
Easy as pie right ?
Don’t quite understand why SharePoint has this ootb behavior, but there will probably be a good explanation.
This is close to what I was looking for. Can't believe we have to find a back door to get this obvious method in check. In my case the form (for case work…legal) has roughly 40+ fields and all are required and of different types.
Do you know of an easier script to handle all Required fields?
On a side note…I hit your site a lot…you have a lot of good information/ideas for SharePoint. Thanks for sharing.
Thanks !
No quick way here to create a PreSaveAction on all required fields..
But perhaps you could build something like:
if the h3 has a *, then take the next formfield and make that required..
What I ended up doing was using the "getTagFromIdentifierAndTitle" function to achieve the result desired (see sample below). You just need to add VAR for all Required Fields desired. I also took this a step further. Since in my case this particular List will get used throughout the Farm, I built my own List Definition that uses it's own RenderingTemplate. I then tweaked the renderingtemplate of the New, Edit, and Display forms to what I need. In this case I include the Presave functions as part of the RenderingTemplate to avoid all the CEWP and form customizations in SP Designer for each list. Wrap it all up as a WSP Solution, deploy and your geared to go. Much easier to upgrade and move to other environments. Thought I would share the final solution in case you ever needed it. This of course could be used in a CEWP as well.
function PreSaveAction() {
var SLT1 = getTagFromIdentifierAndTitle("input","TextField","Nature of Case");
var DESC1 = getTagFromIdentifierAndTitle("textarea","TextField","Current Status");
var DROP1 = getTagFromIdentifierAndTitle("select","DropDownChoice","Type of Case");
if (SLT1.value == "" || DESC1.value == "" || DROP1.value == "")
{
alert("Please fill in all Required Fields");
return false; // Cancel the item save process
}
return true;
}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length – len)) {
return tags[i];
}
}
return null;
}