This is one of my favorite ways for the web application custom validation. And it works for PowerApps as well. We will implement this type of the validation for the First Name and Last Name controls on the Edit form for PowerApp.
The Requirements
- First Name field is a required field.
- Last Name field is a required field.
- Record can’t be saved without those field being populated.
The solution
Hide the Accept icon to prevent the form submission if both fields are not populated.
The implementation
I use a context variable to implement the validation for the Edit screen.
1) Let’s add error labels to the First Name and Last Name cards and change their Visibility based on the control text value to display label if control text value is blank. The error message is not visible initially:
If(IsBlank(dcvFirstName.Text), true, false)
It works nicely however it doesn’t prevent user from submitting the form.
2) Initialize variable. This step is not required in general. A context variable gets set when it’s being used for a first time. However, I added this code to the screen OnVisible action:
UpdateContext( { CheckSum:0 } )
3) For a debugging purpose, let’s add a new label displaying the CheckSum variable value on the screen.
4) We add the OnChange Action for the First Name control to increment the CheckSum variable if the control text is blank:
If(IsBlank(dcvFirstName.Text),UpdateContext( { CheckSum: CheckSum + 1 } ), UpdateContext( { CheckSum: CheckSum - 1 } ))
5) We repeat the same for the Last Name control:
If(IsBlank(dcvLastName.Text),UpdateContext( { CheckSum: CheckSum + 1 } ), UpdateContext( { CheckSum: CheckSum - 1 } ))
6) We update the Visibility property of the Accept icon with the following expression:
If(CheckSum>0,false, true)
The results


