6

I need to make a field required on the edit form. It does not appear on the New Form. I can't set it as required at the site column level because the system will not save the New form without that field completed.

I took this PreSaveAction script from another form I have used it with and tried to modify to fix my issue. I installed it as a script editor web part and when I went to the edit page, it triggers the alert, but won't save when I actually fill in the field.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
function PreSaveAction(){
    //these look for the display name values in the text field
    var textDept1 =  $("select[title$='Dept#1']").val();


    if(textDept1){
        return true;
    }
    else{
        alert("Please insert a Department Number and EOC Code to continue.");
        return false;
    }
}
</script>
  • You are saying that when you edit and make a selection in the Dept#1 field and click save, it does NOT give you the alert, it does exit the form, but doesn't save the value you selected? If not, what happens? – mannaggia Jun 10 '17 at 10:22
  • @mannaggia - It DOES give me the alert if it is empty. But it will also gives me the alert when it is filled and WILL NOT save the form. – cbbrown Jun 12 '17 at 12:41
  • 1
    Before your "if", put an alert(textDept1). If it is null even when filled in, your Jquery selector is wrong. Consider using SPUtility.js - it takes the mess out of getting the right selector. – mannaggia Jun 12 '17 at 13:18
  • @mannaggia - I currently have a SPUtility script setup on my Edit form, but I didn't know that there was a script for making fields required. Do you have a resource I could look at? – cbbrown Jun 15 '17 at 20:57
  • I don't think there is a function to make it required, I was thinking you could use it to get the reference to the field by its display name. – mannaggia Jun 16 '17 at 13:17
6

A simple solution:

We create a list validation that only executes in the edit form, not in the new form. For this, we compare the date of creation with the modification date. If they are equal, it is the new form:

Formula:

=OR(Created=Modified;AND(Created<>Modified;NOT(ISBLANK([test 1]))))

Source with images: Surpoint

  • I changed the column name to Dept1 and placed the edited formula in the column validation, but it is giving me a syntax error when I save. Here's what i put in there: =OR(Created=Modified;AND(Created<>Modified;NOT(ISBLANK([Dept1])))) – cbbrown Jun 12 '17 at 12:59
  • Try changing de ";" by ",". It is depending on the language. – Juan Pablo Pussacq Laborde Jun 12 '17 at 13:18
  • I think I found the problem. I was placing it in the column validation for "Dept1". Once I place it in the Validation Settings for the list, it seems to work. I've only placed it in my test list so far, but fingers crossed it will work the same for the main list. – cbbrown Jun 12 '17 at 13:25
  • Yes, it is in validation list! – Juan Pablo Pussacq Laborde Jun 12 '17 at 13:30
  • Let me ask you another question if you don't mind. Can I use two formulas in the validation list? For instance if I want to use the same formula, just for two different fields. Ex. [Dept]&[EOC1] – cbbrown Jun 12 '17 at 13:32
1

You can check whether the form is a edit/new/disp form. Accordingly you can set the required field.

For edit form the default url of the list for is editform.aspx, so check if the URL is EditForm.aspx then get the field and make it mandatory. Check if this works for you.

There is other option if you are editing the form in InfoPath, its easy there to set the field as mandatory or not with other validations also.

  • Can you elaborate more on this, please? I am aware of the default URL, but confused as to how this would target that field I need to be mandatory. – cbbrown Jun 12 '17 at 12:27
0

you have to modify your code like below.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
function PreSaveAction(){
    //these look for the display name values in the text field
    var textDept1 =  $("select[title$='Dept#1']").val();


    if(textDept1 != "" && textDept1 !=undefined ){
        return true;
    }
    else{
        alert("Please insert a Department Number and EOC Code to continue.");
        return false;
    }
}
</script>
  • I placed this in a Script Editor Web Part, and although it did fire the alert if the field was empty, it also fired when I entered data, and wouldn't save the form. – cbbrown Jun 12 '17 at 12:37
  • Ok do one thing, just place the alert box above the if loop like alert(textDept1) and see what you are being displayed in alert on load? – SharePointMan Jun 12 '17 at 13:50
0

Another option with JSLink:

(function () {
var overrideTest2Ctx = {};
overrideTest2Ctx.Templates = {};
overrideTest2Ctx.Templates.Fields = {
'test_x0020_2': { 'EditForm': hacerRequerido }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideTest2Ctx);
})();

function hacerRequerido(ctx) {

    var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);
    var validators = new SPClientForms.ClientValidation.ValidatorSet();
    validators.RegisterValidator(new SPClientForms.ClientValidation.RequiredValidator());
    formCtx.registerClientValidator(formCtx.fieldName, validators);
    return SPFieldText_Edit(ctx);
}

Source: http://surpoint.blogspot.com.ar/2017/06/metodo-simple-para-hacer-un-campo_12.html

0

Adding the below formula in List validation settings worked for me.

=OR(Created=Modified;AND(Created<>Modified;NOT(ISBLANK([Column Which shouldn't be blank]))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.