A great use case for the new Named Formulas feature of Power Apps is implementing required fields in a form. This blog post will compare how required field implementation can be done without the use of Named Formulas vs. with the use of Named Formulas, highlighting how Named Formulas can speed up development and allow for code reusability.
The Old Way, Without Named Formulas! ☹
Before Named Formulas, implementing required fields in a form was cumbersome at best. The DisplayMode Property of the Submit button usually looked something like this:
If(IsBlank(TextInput1.Text) || IsBlank(TextInput2.Text), DisplayMode.Disabled, DisplayMode.Edit)
As the number of required fields increased, so did the length of the formula in the DisplayMode Property of the Submit button. Making the simple mistake of forgetting to include one of the many IsBlank conditions in any of your app’s Submit Buttons could lead to the submission of incorrect forms down the line and unnecessary time spent debugging your app.
The New Way, With Named Formulas! 😊
Using named formulas allows us to use a short piece of code in the DisplayMode property of the Submit Button. Further, it allows for reusability of the same Named Formula across multiple Buttons on multiple pages. Below is an example of a full implementation of this concept:
Formulas Property of the App Object:
RequiredFields=ForAll([
{error: IsBlank(TextInput1.Text)},
{error: IsBlank(TextInput2.Text)}]
,{Value: ThisRecord});
This code is how we defined the Named Formula, RequiredFields. We define the formula as an array with a property called “error”. If our Text Input control is blank, then the value of the error property is true, and vice versa. In our solution, we will count how many times the Named Formula returns true– this counts how many of the Text Input controls are currently blank.
Content Property of the Badge Control:
CountIf(RequiredFields,Value.error=true)
This code is what displays the number of blank fields in the Badge Control. The CountIf formula counts each time that the “error” property of the RequiredFields Named Formula returns as true. This updates in real-time as text is entered and removed from the Text Input controls.
Visible Property of the Badge Control:
CountIf(RequiredFields,Value.error=true)>0
This code only will display the Badge control if there is at least one blank Text Input control.
Text Property of the “Errors” Text Label Control
If(CountIf(RequiredFields,Value.error=true)=1, “Error”, “Errors”)
This code will display the text “Error” in the Text Label if there is one blank Text Input control and “Errors” if there is more than one blank Text Input control.
Visible Property of the “Errors” Text Label Control:
CountIf(RequiredFields,Value.error=true)>0
This code will only display the “Errors” Text Label if there is at least one blank Text Input control.
DisplayMode Property of the Submit Button
If(CountIf(RequiredFields,Value.error)>0,DisplayMode.Disabled, DisplayMode.Edit)
This code disables the Submit Button if there is at least one blank Text Input control.
BorderColor Property of the Text Input Controls
If(IsBlank(Self.Text), Color.Red, Color.Black)
This code gives the Text Inputs a red border if they are blank.
The Next Time You Implement Required Fields, Remember Named Formulas!
Named Formulas make required field implementation simple, even if there are many required fields in your form. The next time you create a Power Apps form for submission, remember this new feature that’s now in your back pocket.
If you are new to the Power Platform or looking to expand your capacity in this exciting automation space, Compass365 can help. Explore our Power Platform Services or contact us to arrange for a complimentary consultation.
Compass365, a Microsoft Gold Partner, delivers SharePoint, Microsoft Teams and Power Platform solutions that help IT and Business leaders improve the way their organizations operate and how their employees work.