Custom states and validation from error messages in PowerApps

After you created with Custom error messages for data cards in PowerApps your own error message for a data card in your forms component. You can now work with that to handle a custom state and validate on that to enable or disable for example a button. Here is a step-by-step guide how you can do this.

Work with a custom state to validate your form

1. Create a collection on the screen you want to validate a state. Go to the "OnVisible" property and create your state collection.
        Collect(
        DateValidation,
        {
            Name: DataCardKey5.Text,
            Valid: IsBlank(ErrorMessage3.Text)
        },
        {
            Name: DataCardKey4.Text,
            Valid: IsBlank(ErrorMessage2.Text)
        })
2. With the datevalue form field, we will update the state every time we change our date. For that we will choose the "OnChange" property and enter this function.
    Patch(DateValidation, LookUp(DateValidation, MilestoneName = DataCardKey5.Text),
    {Valid: IsBlank(ErrorMessage3.Text)})
3. Select the button you want to activate or deactivate and go to the "DisplayMode" property to check whether one of the validation states is incorrect.
    If(LookUp(DateValidation, Valid=false, false)=false, DisplayMode.Disabled, DisplayMode.Edit)

Each time the view becomes visible, we will create our DateValidation variable. With the OnChange event we will change the value of the field in the collection variable. With the lookup in the DisplayMode property on the button, we will disable or enable the button depending on the validation state of the entire collection