This topic is locked
[SOLVED]

Using "sweetalert2" to confirm "save" data

3/20/2024 10:40:38 AM
PHPRunner Tips and Tricks
D
DRCR Dev author

Refering to this article: https://asprunner.com/forums/forumreplies_list.php?mastertable=forumtopics&masterkey1=29701&orderby=acreated

Hi

The example works beautifully, but my question is.

If the normal save button does not save because all the field have not been selected, how do we STOP the Sweet Alert and allow the user to correct the errors.

Edit Page below:
img alt
The 1 - shows fields that have not been selected, but 2 is already showing. 2 should not show until 1 has been corrected.

This is the First Part of the Tri Part Button.

Swal.fire({
title: 'Creating Custom Period Settings',
html: 'Please wait while we create your custom period settings. This might take a minute or two.',
icon: 'info',
width: '600px', // You can adjust this value as needed
allowOutsideClick: false,
showConfirmButton: false,
willOpen: () => {
Swal.showLoading();
// Triggering the click event on the save button
$('a[id^="saveButton"]').click();
}
});

// Return false to prevent the default action
return false;

Cec

fhumanes 3/20/2024

Hello DRCR Dev:

When the "Save" button is pressed, PHPRUNNER launches the validations at JavaScript level.

In this example, that validation is done when it is executed:

$('a[id^="saveButton"]').click();

I do not know how PHPRUNER's function can be executed for JavaScript's validations (I think it is not documented in the JavaScript API).

To solve your problem, I think the simple thing is to validate that all mandatory fields are completed and then execute the alert.

Greetings,
fernando

D
DRCR Dev author 3/20/2024

To work around needing the popup, I made the long update run by CLI in the background.

I would love to know the answer, because I love the Sweet Alert UI and would love to impliment it site wide for the rare times the server is a little slow. I love the user experience of feeding back to users.

Thank you for your reply, I havnt found the code either to work around, but it would great to know for other parts of my site.

fhumanes thank you so much for all your tutorials - I have used so much in my work. You are a wonderful human and helped me very very much. Bless you for your kindness.

C
cristi 3/20/2024

You can use two methods from sweet alert 2: showValidationMessage and resetValidationMessage for this - I think.
Get the field in jquery and with a conditional check if empty and the sweet alert message will show a validation message at your choosing using show - i never used them but I thinks it worths a shot...

something like

if ($("#field").val() === "") {
swal.showValidationError(
'please enter a value.'
)
}

then reset and display the succes message.

fhumanes 3/21/2024

Hello DRCR dev,

I have already seen how validations can be done before executing the "alert".

This is the code that I put to validate the fields "text1" and "number1".

/* Validation field */
var fields = ['text1','number1'];

var validation = true;
let i = 0;
while (i < fields.length) {
var ctrl = Runner.getControl(pageid, fields[i]);
var status = ctrl.validate();
if (status.result == false) {
validation = false;
}
i++;
}
console.log("validation: "+ validation);

if ( validation ) {
Swal.fire({
title: 'Do you want to save the changes?',
showDenyButton: true,
// showCancelButton: true,
confirmButtonText: 'Save',
denyButtonText: "Don't save",
}).then((result) => {
/* Read more about isConfirmed, isDenied below */
if (result.isConfirmed) {
Swal.fire('Saved!', '', 'success')
// $('#saveButton7').click();
$('a[id^="saveButton"]').click();

} else {
if (result.isDenied) {
Swal.fire('Changes are not saved', '', 'info')
}
}
})
}
return false;

Manual : https://xlinesoft.com/phprunner/docs/ctrl_validate.htm

I will modify my article with these changes.

Greetings,
fernando

D
DRCR Dev author 3/21/2024

Hi fhumanes

Thanks! That makes so much sense. Its the parts controlled by PHPRUNNER that confuse me,

Thank you, I saved this code to my good code list.