Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Controls and Formulas

Forms and Validation in Power Apps

Build Edit and Display forms in Power Apps: DataSource, Item, data cards, SubmitForm, OnSuccess, OnFailure, NewForm, ResetForm and custom validation with Notify.

Forms collect and edit records. The Edit form control is a shortcut: you point it at a data source, choose fields and it creates data cards (one per column) and the plumbing to save. This lesson shows the formulas around it and how to validate input.

Two form controls

  • Edit form: creates, edits and saves records.
  • Display form: shows a record read-only.

Both use the same properties: DataSource, Item, Fields and layout settings. In Studio, insert an Edit form from Insert, Input, Edit form, then in the properties pane choose the data source and Edit fields to add columns.

DataSource, Item and Mode

TEXT
frmTask.DataSource: Tasks
frmTask.Item: galTasks.Selected

Item decides which record the form shows. The form's Mode is FormMode.Edit, New or View. You change it with functions:

  • NewForm(frmTask): blank record, mode New.
  • EditForm(frmTask): switch to Edit for the current Item.
  • ViewForm(frmTask): switch to read-only.
  • ResetForm(frmTask): discard unsaved changes.
  • SubmitForm(frmTask): validate and save.

A single form can serve add and edit screens:

TEXT
// "Add" button
NewForm(frmTask); Navigate(scrForm)
// "Edit" button
EditForm(frmTask); Navigate(scrForm, ScreenTransition.None)

Submitting and results

SubmitForm saves data to the source asynchronously. Use the form's events to react:

TEXT
OnSuccess: Notify("Saved", NotificationType.Success); Back()
OnFailure: Notify("Could not save: " & frmTask.Error, NotificationType.Error)

After success, frmTask.LastSubmit holds the saved record, with its new ID. Do not call Back() in the button's OnSelect right after SubmitForm, because the save has not finished yet; use OnSuccess.

Data cards

Each field is a data card containing controls:

  • DataCardKey (the label),
  • DataCardValue (the input, like DataCardValue1.Text),
  • an error message label showing the data source's validation text.

Useful card properties: Required (user must enter a value), DisplayMode (lock a field), Default (initial value), and Update (the value written back). You can unlock a card to change its controls, for example swap a text box for a dropdown, and set its Update to drpStatus.Selected.Value.

Validation

Validation happens at three levels:

  1. Data source: required columns, types and lengths enforced by Dataverse or SharePoint.
  2. Card: the Required property; a red message shows when blank.
  3. Your formulas: custom rules before SubmitForm.

Custom rules often use an error label and a disabled button:

TEXT
// lblError.Text
If(IsBlank(DataCardValue_Title.Text), "Title is required",
   Len(DataCardValue_Title.Text) > 100, "Title is too long", "")

// btnSave.DisplayMode
If(lblError.Text = "", DisplayMode.Edit, DisplayMode.Disabled)

You can also check frmTask.Valid, which is true when all cards pass their rules.

The stand-in below implements a validation function the way you might lay out the rules, returning one message per field.

JS Stand-in: field-level validation rules

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Forms without the form control

For full control, skip the form and use inputs plus Patch:

TEXT
Patch(Tasks, Defaults(Tasks), {
  Title: txtTitle.Text,
  Due: dpDue.SelectedDate,
  Done: false
})

This gives exact validation and error handling. Check the result with IfError:

TEXT
IfError(
  Patch(Tasks, Defaults(Tasks), {Title: txtTitle.Text}),
  Notify("Save failed: " & FirstError.Message, NotificationType.Error),
  Notify("Saved", NotificationType.Success)
)

IfError evaluates the first formula; if it errors, it runs the second argument. The next stand-in models that flow.

JS Stand-in: IfError around a save

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Unsaved changes

Prevent lost work: frmTask.Unsaved is true when there are pending edits. Use it before leaving:

TEXT
If(frmTask.Unsaved, Set(varConfirmLeave, true), Back())

and show a confirmation dialog based on varConfirmLeave.

Common mistakes

  • Calling Back() immediately after SubmitForm. Use OnSuccess.
  • Setting Item to a collection record that does not match the data source's key.
  • Forgetting Update when replacing a card's input; the form then saves the old value.
  • Putting NewForm in OnVisible, which resets the form every time you return to the screen.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function saves an Edit form's data?

    SubmitForm validates and writes the record to the data source.
  2. 2. Where should you navigate back after a successful save?

    SubmitForm is asynchronous, so react to OnSuccess or OnFailure.
  3. 3. Which property sets which record a form shows?

    Item is the record the form displays or edits.
  4. 4. When you replace a data card's input control, what must you update?

    Update defines the value the card writes back to the data source.

Summary

  • Edit forms bind to a data source and record; use NewForm, EditForm and SubmitForm.
  • React to OnSuccess and OnFailure; show messages with Notify.
  • Validate with data source rules, card Required and your own formulas.
  • Use Patch with IfError when you need full control.

Next: Delegation and data sources