Business Applications, dynamics 365, PowerApps, Uncategorized

Implementing a web form check sum group validation for PowerApps

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.

pa1

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.

pa2pa3

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 } )

pa4

3) For a debugging purpose, let’s add a new label displaying the CheckSum variable value on the screen.

pa5

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 } ))

pa6

5) We repeat the same for the Last Name control:

If(IsBlank(dcvLastName.Text),UpdateContext( { CheckSum: CheckSum + 1 } ), UpdateContext( { CheckSum: CheckSum - 1 } ))

pa8

6) We update the Visibility property of the Accept icon with the following expression:

If(CheckSum>0,false, true)

pa9

The results

pa12
The icon appears only if both fields are populated
pa11
Both fields are not populated. The CheckSum is 2. The Accept icon is not Visible.
pa10
One field is not populated. The CheckSum is 1. The Accept icon is not Visible.

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s