Using Named Formulas to Validate Data in Power Apps
Nikki Czachowski, Systems Developer at Compass365 walks you through a simple application of Power Apps Named Formulas to validate data.
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.
Transcript:
0:00
Hey guys today I’m going to be demonstrating a really short, simple but extremely useful application of Power Apps named Formulas.
0:08
So for this implementation I’m going to be doing a data validation on a control using a named formula.
0:15
So first I’m just going to show you guys what I have, how my implementation is functioning.
0:19
Right here you can see that I’m using one of the new modern text input controls and this right here is just a regular non modern text label that is displaying my error message.
0:30
So my error message is going to be displaying anytime that the value of this modern text input control is not a 10 digit number.
0:39
I can think of this being used in an application where you need users to type in for example, a phone number and not type in any spaces, any alphabetical letters, no special characters, just simply 10 digits.
0:51
So this validation is a pretty common one and I think it has a lot of potential to be used in a lot of applications that you may be building.
0:59
So let me just demonstrate exactly how this thing works.
1:01
It’s not super complex.
1:04
All it is is my text label displaying the error message or not displaying the error message based on what is in my text input controls value gonna start typing numbers.
1:16
You can see that in real time it still knows that I haven’t hit 10 digits yet, and right when I hit 10 digits and there’s nothing else in there, my error message goes away.
1:25
So that’s real time data validation.
1:27
You can also be toggling things such as display modes of buttons, making them disabled.
1:33
You could make you know the movement to other screens disabled based on an air data validation like this.
1:41
I just chose to keep it really simple and just toggle the visibility of my text label that you can see is currently not visible.
1:48
So let me go ahead and demonstrate what happens if I actually type 11 digits instead of 10.
1:53
You can see my error message comes right back up.
1:56
Let’s go back to 10.
1:57
I’m going to type a space.
1:59
That’s wrong too because I do not no longer have 10 digits.
2:02
I have 10 digits in space, so that is accurate.
2:05
Gonna type a special character?
2:06
Nope.
2:07
A letter?
2:08
Nope.
2:09
The only thing that this thing wants to take is 10 digits.
2:12
So that is the basic implementation.
2:15
Now I’m gonna show you guys the code of how this thing works.
2:19
So I’m going to go whoops to my app object.
2:22
I go to the formulas property and this is actually a name formula that I have been using for something else.
2:29
So I’m going to ignore this for now and Scroll down to the one that we’re filming this video on, which is a formula that I named Validate.
2:36
And I plan to use this formula for a bunch of different types of data validation requirements across my app.
2:42
And I can actually do that by just adding fields to my formula.
2:47
So this field or records, excuse me, this record, corresponds to that single implementation of data validation for that control that I just showed you.
2:57
If I have a ton of controls and they all have different data validation needs, I could just keep adding records to this named formula and I could I could use the same named formula but apply different validations across different controls.
3:11
So that’s why I think that this application is super versatile.
3:14
It’s just super awesome and I love it.
3:17
So the basic structure of this name formula is a for all, and within it, like I was saying, we’re going to have records.
3:23
For now, I just have one record.
3:24
Within my record I have a property called Field and a property called Error.
3:29
My Field property is basically functioning as a primary key or an identifier.
3:34
This would be really useful if I was doing data validations on many controls.
3:39
I’m currently only using it on one control so it’s not that useful, but it’s still there.
3:43
It’s still the bones of this if I want to expand it out in the future by adding more data validations.
3:50
So for now I’m just calling my modern text input control field one in my field key in the air property.
3:58
This is actually going to return a boolean, so this is going to return a true or false.
4:03
I define this property by using the is match function and what the is match function is saying in.
4:10
In layman’s terms is it’s asking whether the value of my modern text input control follows this pattern or matches this pattern.
4:22
This is a regular expression and this is saying that it.
4:29
The regular expression is saying to only include 10 digits.
4:33
No special characters, no spaces, no alphabetical letters.
4:37
Regular expressions are really awesome to use for data validation and I can’t say that I’m a self-proclaimed expert on them.
4:45
If you just Google regular expression generator or regular expression for your specific situation, you’re probably going to find something that you can test and validate that it works.
4:54
And then there you go, you have your regular expression that works for you.
4:58
This is the one that I found really works for limiting to only 10 digits.
5:02
So this has been tested by me and it works great.
5:06
Notice that I have this! Here right outside of my is match.
5:10
So that is basically saying that this error property is going to return true if the is match is false.
5:18
So I’m going to return true for error if the value of my modern text input control does not match my pattern of 10 digits.
5:30
If it has anything else, anything extra, anything wrong, it is going to return as true that is in an error state.
5:38
At the bottom of the for all you can see that I am just returning the record.
5:45
So I am spitting out this whole record when I call my function and I am able to make decisions based on the values of whatever is in that record.
5:56
Okay.
5:57
So now I’m going to be showing you guys how this implementation actually works.
6:00
So I’m selected on my text label that is displaying my error message and I’m toggling the visibility property of this text label.
6:08
I’m using the count in formula and my source is actually my named formula called Validate.
6:14
So I’m going to be counting if the field one or the field property of my Name formula equals field one.
6:23
That’s like my key indicating that yes, I am talking about this modern text input and the error property is true.
6:33
So if you guys remember my error property is a boolean so this is just telling me that error is true.
6:42
And so you guys can see that if I highlight my name, highlight my name formula you can see what I was referring to, it actually spits out one singular column called value and within that is my singular record that I put in which contains the field and error properties.
6:58
So all this is saying is that I’m counting something.
7:02
If my name formula is saying that my text modern text input control that I called field one is in an airstate, that means it is not matching that regular expression.
7:15
So you can see right now that the value of my count F = 0 because currently this air property is evaluating to false because it is not in an airstate.
7:25
If I do something to make it become into the airstate such as insert the 11th digit, we can go back on this formula and see that now it is evaluating to one.
7:39
And I set this up so that if the the only two options are one are one and 0 so anything greater than 0, the only other option is 1 is going to trigger this to be visible.
7:52
And that is essentially how this whole thing works.
7:55
That was a really really simple, easy quick way to use named formulas and you can actually apply this across a bunch of different controls in your apps using a bunch of different data validations.
8:06
And I just really love this application.
8:08
So thanks for sticking with me and watching this video and I hope that you guys can use this in the future and get something beneficial out of it.
8:15
Thanks.