0

I have a choice column which has 4 values in the drop-down list and based on each value I am hiding other columns.

Out of these hidden columns some columns are mandatory, and I am not able to save the form as it gives Cannot leave blank for those columns.

But since these are hidden ideally it should not pop up.

Can any one please help me out?

8
  • If the fields are required (in list settings) then you have to pass some value to those fields. you cannot leave them blank even if those are hidden fields. Commented Jun 11, 2020 at 6:53
  • Hello @GaneshSanap , these mandatory fields are not required for that value which i selected so i have hidden them , it is required for second value in my drop down list. can you please suggest any alternataive Commented Jun 11, 2020 at 7:28
  • Are you using multiple content types in your list? Or have you customized list forms? Commented Jun 11, 2020 at 8:20
  • Yes i have allowed management content type to "Yes" in the list settings. I do not have customized list forms . It is default form of share point Commented Jun 11, 2020 at 8:52
  • If you need some fields to be required only if the specific choice is selected from dropdown then you have to do some customization's in list forms. Which version of SharePoint you are using? If you are using SharePoint on-premise then you can use custom javascript or if you are using SharePoint Online then you can achieve this using powerapps very easily. (In both cases you don't make the fields required from column settings). Commented Jun 11, 2020 at 8:56

2 Answers 2

0

Don't make the columns as required in the list settings. Use custom JavaScript to check the selected drop-down value.

Based on the selected value you can make other columns as required or show it on the list form. Else remove the required condition and hide those columns.

By this, it will not show the error popup and form will be saved.

Hide/Show columns in Custom List based on dropdown selection

To mark the field as required:

Consider Column name as Column1 having ID Column1ID. Use following script to make it as a required field.

var column1 = document.querySelector("div[id^='Column1ID][id$='$TextField_inplacerte']";
var column1TR = column1.parentElement.parentElement.parentElement.parentElement ;
var column1Label = column1TR.querySelector("nobr") ;

// Add Required Asterisk to the Label
var starElement = document.createElement("span") ;
starElement. title = "This is a required field." ;
starElement.textContent = " * " ;
starElement.className = "ms-accenText" ;

column1Label.appendChild(starElement) ;
4
  • Hello , I have hide/show the columns based on java script only , can you please tell me out of those columns which i am showing how to make it required for few? Commented Jun 11, 2020 at 9:08
  • Check uppdated answer having steps to mark column as required. Commented Jun 11, 2020 at 9:41
  • Should i write this inside pre save function? as of now i am using ready function to hide or show , Also i am not sure of the class name , can u pls confirm Commented Jun 11, 2020 at 9:51
  • Yes, you need to validate the fields (if they are blank or not) in PreSaveAction function (This function executes on Save button click and before saving item in list). Check the reference links I provided in above comments for more information. Commented Jun 11, 2020 at 11:21
0

If the fields are required (in list settings) then you have to pass some value to those fields. you cannot leave them blank even if those are hidden fields.

So, do not make the fields required from list settings.

If you need some fields to be required only if the specific choice is selected from dropdown then you have to customize the list forms using JavaScript/JQuery.

For Example:

Consider, you have a choice field with its display name as Information Type and one date field (named Review Date).

And you want to make Review Date as required field only if the "Client Lost" or "Client Retained" option is selected from Information Type dropdown.

Then you can use PreSaveAction() like below:

function PreSaveAction() {
    var returnValue = true;
    returnValue = returnValue && ValidateReviewDate();
    return returnValue;
}

function ValidateReviewDate() {
    var ddlInformationType = $("select[title^='Information Type']")[0];
    var txtReviewDate = $(":input[title^='Review Date']")[0];

    if(ddlInformationType[ddlInformationType.selectedIndex].value=="Client Lost" || ddlInformationType[ddlInformationType.selectedIndex].value=="Client Retained" ) {
        if(txtReviewDate.value.length == 0) {
            alert("Please enter Review Date");
            txtReviewDate.focus();
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}

Note: Make sure you have added reference to JQuery file before using above code as it using JQuery ($) selectors to access the fields on form.

3
  • HI @Ganesh Sanap I tried the below **function PreSaveAction() { if($("select[title='Request Form Type']").val()=='Tracking Number request') { if($("select[title='Project Manager']").val()==0) { alert('Project Manager field is mandatory'); return true; } } } ** Commented Jun 12, 2020 at 10:48
  • This will not work. The idea of using PreSaveAction is, you need to check all the conditions and if you are OK with saving the list item then you need to return true and if there is any problem or you need to force the validation, you do not want to save list items then return false from PreSaveAction function. Commented Jun 12, 2020 at 10:55
  • Hello @GaneshSanap , I am new to sharepoint 2013 now i am try using the below code but when i click on "Save" it is neither saving now it is getting the alert. _ function PreSaveAction() { if($("select[title='Request Form Type']").val()=='Tracking Number request') { if($("select[title='CGI Project Manager']").val()=='') { alert('CGI Project Manager field is mandatory'); return false; } else { return true; } } } _ Commented Jun 12, 2020 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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