Answered by:
Conditionally Cancel Navigation Before Apply Changes

Question
-
Hello,
I am trying to build in some validation, and cancel all navigation and/or applying of changes on a dialog AddEditScreen, depending on the value of a specific content item.
I am also using SweetAlert. Here is my code...
myapp.AddEditIncident.beforeApplyChanges = function (screen) { if (screen.Incident.StrRepChargeTo == null) { swal({ title: "Make a selection!", text: "Please choose a Charge To location...", type: "success", showCancelButton: false, showConfirmButton: true, cancelButtonText: "OK!", confirmButtonText: "OK!" }, function () { //myapp.cancelChanges().then(function () { // myapp.navigateBack(); }) }) } }
Everything works, except LS starts the page navigation even before the beforeApplyChanges function is run.
The idea is that if StrRepChargeTo is null, LS will stop all navigation, stop all applying changes, show the SWAL alert, allow the user to click 'OK!', and the simply return to the AddEditIncident dialog screen so the user can make a selection.
The commented out parts at the end of the code are things I have tried, but I think it is being read too late.
Any advice appreciated.
Thank you!
Tuesday, October 18, 2016 8:14 PM
Answers
-
***Update***
I found a work around to get this working by data binding the change handler to the contentItem, to reset the validation result when the entity is changed.
myapp.AddEditIncident.StrRepChargeTo_postRender = function (element, contentItem) { contentItem.dataBind("screen.Incident.StrRepChargeTo", function (value) { contentItem.validationResults = []; }); } myapp.AddEditIncident.beforeApplyChanges = function (screen) { if (screen.Incident.StrRepChargeTo == null) { screen.findContentItem("StrRepChargeTo").validationResults = [ new msls.ValidationResult( screen.Incident.details.properties.StrRepChargeTo, "Please select a 'Charge Repair To:' location..." ) ]; return false; } }
Thank you
Monday, October 24, 2016 8:32 PM
All replies
-
Hi CreedCor,
AFAIK, we can use myapp.commitChanges() to do function below:
- Run validation on the current active screen tab.
- If no validation errors, call data service saveChanges()
- Go back to previous screen.
To cancel navigation away from the page, this thread shows you how to do that.
Best regards,
Angie
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, October 21, 2016 10:32 AM -
Thank you for your reply.
I ended up applying custom validation per this tutorial...
https://msdn.microsoft.com/en-us/library/jj733572.aspx
Thank you!
Monday, October 24, 2016 3:53 PM -
Hi CreedCor,
AFAIK, we can use myapp.commitChanges() to do function below:
- Run validation on the current active screen tab.
- If no validation errors, call data service saveChanges()
- Go back to previous screen.
To cancel navigation away from the page, this thread shows you how to do that.
Best regards,
Angie
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Thank you again for your reply. I am using this script to handle validation on this entity...
myapp.AddEditIncident.beforeApplyChanges = function (screen) { // Write code here. if (screen.Incident.StrRepChargeTo == null) { screen.findContentItem("StrRepChargeTo").validationResults = [ new msls.ValidationResult( screen.Incident.details.properties.StrRepChargeTo, "Please select a 'Charge Repair To:' location..." ) ]; return false; } }
It does detect the validation error, but it does not detect when this condition is no longer true.
For example:
Open the dialog screen > field is null > try to apply changes > receive validation error > put a value in the field > receive same validation error.
When I open the dialog screen and select a value BEFORE trying to apply changes, the logic works and the changes are applied.
Thank you for any advice
- Edited by CreedCor Monday, October 24, 2016 5:18 PM
Monday, October 24, 2016 5:17 PM -
***Update***
I found a work around to get this working by data binding the change handler to the contentItem, to reset the validation result when the entity is changed.
myapp.AddEditIncident.StrRepChargeTo_postRender = function (element, contentItem) { contentItem.dataBind("screen.Incident.StrRepChargeTo", function (value) { contentItem.validationResults = []; }); } myapp.AddEditIncident.beforeApplyChanges = function (screen) { if (screen.Incident.StrRepChargeTo == null) { screen.findContentItem("StrRepChargeTo").validationResults = [ new msls.ValidationResult( screen.Incident.details.properties.StrRepChargeTo, "Please select a 'Charge Repair To:' location..." ) ]; return false; } }
Thank you
Monday, October 24, 2016 8:32 PM