More ways to work with errors in PowerApps

In the last post, Handling Errors in PowerApps, I discussed a way to prevent errors by disabling submit buttons until all required fields are completed and how to display custom error text for a data card based upon another data card’s value.

That post spawned some questions which are the focus of this post. In the last post, I mentioned that the built-in error contains a not-so-friendly message. Let’s say that the you want custom error text but it isn’t based upon the value of another data card. Maybe it is a required field that was left empty. You just want custom text to appear below that field.

Change the default error text for empty required fields

Two empty required fields: One with a custom error message and one with the default error message.

In the example above, the top field has a custom error message. The bottom field displays the default error message. Both errors appear because the corresponding fields were empty.

Here’s the code I used to display the custom error in the corresponding label control’s Text property:

If(Not(IsBlank(Parent.Error)),"Not so fast. This is a required field.")

How does this work? Well, this all hinges on Parent.Error. Does this data card’s Error property contain text or is it blank. If the Error property is blank (because the require field is not empty), no error should be displayed. If the data card’s Error property is not blank, i.e., some text is present in the Error property, display the custom text instead of whatever is normally displayed.

Multiple custom errors for a single data card

The next question was about multiple custom errors for a single data card. Let’s look at the previous example, again.

Bottom field displaying default error text

Bottom field displaying custom error based upon value of preceding field

The desire is to have both, a custom error based upon the validity of text entered and a different custom error if the field is left empty. This is achievable by simply nesting the statements.

Here is the code I used to make it happen:

If(‘DamagedSections_DataCard2'.Update>’TotalSections_DataCard2'.Update,"The number of damaged sections may not exceed the number of total sections inspected.",If(Not(IsBlank(Parent.Error)),"Not so fast. This is a required field."))

Feel free to swap your data card names with mine. Now, end-users see different custom error messages for a single data card.