We sometime run into Form Validation in Salesforce Standard Page Layout that requires us to think a bit harder.

The requirement was to have the user to either fill out all the fields in the section or leave the entire section empty.

In other words, if the user starts to input information into the section, all the fields are required in order to submit the form. However, the user has the option to leave all the fields empty and that would be acceptable to submit the form.

Let’s break this down.

Custom Fields

I have 3 custom fields added to Contact Standard Object. The goal is for user to either fill out all the Employment information or leave the whole section blank.

 

Validation Rule Part #1

First part of the Validation formula is to handle the case of all fields need to be filled out. The strategy here is get the formula to return True if the validation requirements. Since validation rule needs True for error, we simply negate the entire formula with NOT operator

NOT(
   AND(
      NOT(ISBLANK(Employer__c)),
      NOT(ISBLANK(Employment_Years__c)),
      NOT(ISPICKVAL(Employment_Type__c, ''))
   )
)

If you leave any fields blank, validation rules will kick in and will show you the validation error

 

Validation Rule Part #2

However, we want to allow a case where none of the fields are filled out. With Validate Rule Part #1 alone, we cannot achieve this.

So we add the second part to this formula. This formula alone will enforce user to leave all the fields empty. If any of the fields are filled out, validation error will occur.

NOT(
   AND(
     ISBLANK(Employer__c),
     ISBLANK(Employment_Years__c),
     ISPICKVAL(Employment_Type__c, '')
   )
)

 

Putting them together

We need to combine them together to have the validation rule we want.

AND(
    NOT(
        AND(
           NOT(ISBLANK(Employer__c)),
           NOT(ISBLANK(Employment_Years__c)),
           NOT(ISPICKVAL(Employment_Type__c, ''))
        )
    ),
    NOT(
      AND(
           ISBLANK(Employer__c),
           ISBLANK(Employment_Years__c),
           ISPICKVAL(Employment_Type__c, '')
      )
    )
)

Hope this can save you time if you need to implement this type of form validation.