Question

I have to customize the NewForm and EditForm using JSLink. Perhaps there is another way of doing this.

So what I would like to know are these:

  1. How can I make a field required or not depending on condition? How do I set an asterisk next to the name of the field?

  2. How do I hide the entire row of the column, including the text depending on the condition?

So far I am using this blog

Was it helpful?

Solution

1st answer: This makes the field required and adds asterisk next to the column name.

  1. Go to Advanced Settings and Enable the Allow management of content types.

  2. Now you will see the content types associated with the list under the Settings section.

  3. Click on Item.

  4. Select the column.

  5. Check the Required radio button.

2nd answer:

if(condition){
 $('nobr:contains("Column")').closest('tr').hide();
}

Update to answer 1:

function PreSaveAction() { 
var reqfield = $("input[title='Required Column']").val(); 
 var status = $("input[title='Status Column']").val();
 if(status == 'condition'){ 
   if(reqfield== ""){ 
   $('nobr:contains("Required Column")').append('<span title="This is a required field." class="ms-accentText"> *</span>');              
   $("input[title='Required Column']").focus();        
   return false;
  }else{
     return true;
  }
 }else{
     return true;
 }
}

OTHER TIPS

JSLink is usually for displaying content in a different matter. What you may be looking for is to create a custom "NewForm" and "EditForm" using SharePoint Designer. Just open your site in SharePoint Designer, navigate to your list and create two custom forms. One for NewForm, and one for EditForm. Then you can add jquery and javascript logic to make a field required based on condition using the PreSaveAction method. Here is an example.

<script type="text/javascript">
 var $j = jQuery.noConflict(); 
 function PreSaveAction() { 
   alert("here");
   var txtStatus = $j(":input[title='Status']").val(); 
   if(txtStatus == "In Progress"){        
   alert("Invalid Status");        
   var statusfocus = $j(":input[title='Status']");        
   statusfocus .focus();        
   return false;}
   return true;
  }
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top