Note: this answer works as long as none of the fields in question are any of the following types:
- Multiple lines of text
- Multiple Choice (i.e. checkboxes; radio button/dropdown is OK)
- Lookup
- Person/Group
- Hyperlink
- External Data
- Managed Metadata
If your conditionally-required fields are of one of these types, see Sudhir's answer.
You could use jQuery and/or JavaScript to hide/validate the fields, display validation messages, etc., but that's kind of like killing a fly with a bazooka. A much simpler solution is to use the OOTB List Validation Settings to check for a particular combination of field values.
In your example, you want Evidence and Comments to be optional, unless the task's Status is Completed
. This can be achieved with the following list validation formula:
=IF(Status="Completed",NOT(OR(ISEMPTY(Evidence),ISEMPTY(Comments))),TRUE)
What's going on here? Let's break it down:
OR(ISEMPTY(Evidence),ISEMPTY(Comments)) // evaluates to TRUE if either field is empty
NOT(OR(...)) // inverts the above result - FALSE if either
// field is empty
IF(Status="Completed",NOT(...),TRUE) // if Status is "Completed", evaluates the NOT
// function above. Otherwise, returns TRUE
When the user tries to save the form, if the list validation formula returns TRUE
, the operation completes normally. But if the validation formula returns FALSE
, the user will not be able to commit the changes. Enter an explanation, such as "Completed tasks require information in both the Comments and Evidence fields." into the User Message box in the List Validation settings page.