This situation can be managed by combining the use of variables and conditions that dynamically check if the necessary criteria are met before enabling the Submit button. Below, I will describe a method to accomplish this, including how to enable/disable the Submit button based on the Save status of the current line in the Gallery and provide a Notification if the user tries to submit without saving the current line.
1. Managing Save Status in the Gallery:
Ensure that each line/item in the Gallery has a property or flag to indicate if it has been saved. This can be managed using a boolean variable for simplicity.
2. Update This Variable on Save:
When a user saves the current line, update the variable associated with that line to indicate it is saved. Use the `OnSave` property of the Save button to set this flag.
3. Check If All Necessary Conditions Are Met:
Use the `Submit` button's `DisplayMode` property to dynamically enable or disable the button based on the saving status of the lines in the Gallery.
Here is an example of how you can set this up:
Step 1: Add a SaveStatus Flag to Each Item in the Gallery
Suppose your Gallery Items are collected in a collection named `colGalleryItems`. You can initialize a flag called `SaveStatus` when adding new items to this collection.
```plaintext
// Add new items with SaveStatus as false initially
Collect(colGalleryItems, { Field1: "", Field2: "", SaveStatus: false });
```
**Step 2: Update SaveStatus on Save Button Click
Set the `OnSelect` property of the Save button to update the `SaveStatus` flag of the current item.
```plaintext
UpdateIf(colGalleryItems, ThisItem.ID = ID, { SaveStatus: true });
// Additional logic for saving the data if required
```
**Step 3: Enable/Disable Submit Button Based on Save Status**
Check the `SaveStatus` of all items in the collection, and enable/disable the Submit button accordingly.
```plaintext
// This function checks if all items have SaveStatus as true
IsAllItemsSaved(colGalleryItems) ::= All(colGalleryItems, SaveStatus);
// Use this function in the DisplayMode property of the Submit button
SubmitButton.DisplayMode ::= If(IsAllItemsSaved(colGalleryItems), DisplayMode.Edit, DisplayMode.Disabled);
```
Step 4: Notify User to Save First Before Submit
You might want to provide feedback in case the user tries to click the Submit button when it's not enabled. You can add this logic in the `OnSelect` property of the Submit button as follows:
```plaintext
SubmitButton.OnSelect ::=
If(
IsAllItemsSaved(colGalleryItems),
// Submit logic here
SubmitForm(YourForm),
Notify("Please save all changes before submitting.", NotificationType.Error)
);
```
### Example Code Combining All Steps in PowerApps
Here’s a more summarized flow:
```plaintext
// Initialization when adding new items
Collect(colGalleryItems, { Field1: "", Field2: "", SaveStatus: false });
// OnSelect of Save button
SaveButton.OnSelect ::=
UpdateIf(colGalleryItems, ThisItem.ID = ID, { SaveStatus: true });
// Submit button properties
SubmitButton.DisplayMode ::= If(All(colGalleryItems, SaveStatus), DisplayMode.Edit, DisplayMode.Disabled);
SubmitButton.OnSelect ::=
If(
All(colGalleryItems, SaveStatus),
SubmitForm(YourForm),
Notify("Please save all changes before submitting.", NotificationType.Error)
);
```
By implementing these steps, you ensure that the Submit button is interactive only when all conditions are favorable, and you can guide the user with notifications when they attempt to submit without saving necessary changes.
If you had further queries reply or Accept as solution.
Thanks!