Forum Discussion

Shaune1215's avatar
Shaune1215
Copper Contributor
Apr 14, 2024

Conditional formatting for blank FORM fields

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "box-sizing": "border-box",
    "padding": "0 2px",
    "overflow": "hidden",
    "text-overflow": "ellipsis"
  },
  "attributes": {
    "class": {
      "operator": ":",
      "operands": [
        {
          "operator": "==",
          "operands": [
            "[$SingleLineOfText]",
            ""
          ]
        },
        "sp-css-backgroundColor-BgGold",
        ""
      ]
    }
  },
  "txtContent": "[$SingleLineOfText]"
}

 

Conditional formatting works great on list views using the code snippet above; however, It does not work for any of the form views.  I've tried multiple methods to try to highlight a blank field (number or text) on a form field but nothing has worked so far.  Do you know why this is?

 

List View with formatting looks great.

 

Form View formatting is not present.  Instead SP displays an em dash?

 

 

If I change the formula to look for any value, for example "test123", the formatting does show up on the form view.  The only thing that does not work is blank fields on the form view.  Any advice?

3 Replies

  • Tom_B490's avatar
    Tom_B490
    Copper Contributor

    Shaune1215 

    I encounter the same problem when attempting to insert text if a date field is empty.

     

    {
        "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
        "elmType": "div",
        "txtContent": "=if(@currentField, @currentField.displayValue, 'unset')"
    }

     

    Unfortunately, it appears that Microsoft has chosen to apply custom formatting in a form view only when the field contains data. The sp-custom-formatted-ReactFieldEditor-Core-Display element does not activate when the field is empty.

     

    We will have to wait until Microsoft alters this behavior.

    • SDawson's avatar
      SDawson
      Copper Contributor

      The below information is found here:  https://ganeshsanapblogs.wordpress.com/2021/06/20/sharepoint-json-formatting-check-if-date-and-time-column-is-blank-empty/ 

      to check if date & time column is blank/empty using SharePoint JSON formatting, you have to use either of below workarounds:

      Using Number() function

      You can use Number(DateTimeColumn) == 0 to check if the date & time column is blank or not.

      Example

      { "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "=if(Number([$DueDate]) == 0, 'Blank Date', if([$DueDate] < @now, 'Expired', 'Active'))", "attributes": { "class": "=if(Number([$DueDate]) == 0, 'sp-field-severity--warning', if([$DueDate] < @now, 'sp-field-severity--blocked', 'sp-field-severity--good'))" } }

      Where [$DueDate] is an https://ganeshsanapblogs.wordpress.com/2023/04/17/how-to-find-the-internal-name-of-sharepoint-columns/.

       

      You can also find above JSON at list formatting samples on GitHub: https://github.com/pnp/List-Formatting/tree/master/column-samples/date-check-blank-format

      Using toString() function

      You can use toString(DateTimeColumn) == '' to check if the date & time column is blank or not.

      Example

      { "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "=if(toString([$DueDate]) == '', 'Blank Date', if([$DueDate] < @now, 'Expired', 'Active'))", "attributes": { "class": "=if(toString([$DueDate]) == '', 'sp-field-severity--warning', if([$DueDate] < @now, 'sp-field-severity--blocked', 'sp-field-severity--good'))" } }

      Simplest way

      You can also check if date & time column is blank/empty using below JSON:

      { "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "=if(@currentField, @currentField, 'Blank Date')", "attributes": { "class": "=if(@currentField, 'sp-field-severity--good', 'sp-field-severity--warning')" } }

       

      SharePoint JSON formatting – Check if date and time column is blank or empty

       

      Learn More

      • https://ganeshsanapblogs.wordpress.com/2021/01/10/working-with-sharepoint-online-microsoft-list-comments-using-json-formatting/
      • https://ganeshsanapblogs.wordpress.com/2022/08/22/sharepoint-replace-all-occurrences-of-substring-in-a-string-using-json-formatting/
      • https://ganeshsanapblogs.wordpress.com/2022/08/23/sharepoint-highlight-selected-list-item-row-using-json-formatting/
      • https://ganeshsanapblogs.wordpress.com/2022/11/12/download-image-from-sharepoint-image-column-using-json-formatting/
      • https://ganeshsanapblogs.wordpress.com/2022/11/10/sharepoint-online-download-files-using-json-formatting/
  • Shaune1215 Yeah, I have also observed this behavior in SharePoint list forms.

     

    Microsoft show "-" by default for empty single line of text fields. Maybe because of that the formatting is not correctly shown on the list forms.

     

    Try raising a support ticket with Microsoft directly and report this behavior: Get M365 Support - Online Support 


    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Resources