Issue
I want to do a double confirmation when I want to delete an account, I really struggle for this. I checked on the forum but nothing seem to be like my problem. Im using Symfony
My function deleteAccount on my UserAccountController :
/**
* @Route("user/account/delete", name="user_delete")
*/
public function deleteAccount(): Response{
$user = $this->getUser();
$this->container->get('security.token_storage')->setToken(null);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($user);
$entityManager->flush();
$this->addFlash(
'success',
'Account Deleted successfully'
);
return $this->redirectToRoute('homepage');
}
My link on the view :
<a href="{{ path('user_delete') }}" class="btn btn-danger deleteButton">Delete Account</a>
And my jquery :
$('.deleteButton').on('click', function() {
event.preventDefault();
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
setTimeout( function () {
$.get($(this).attr('href'),{});
}, 1200);
Swal.fire(
'Deleted!',
'',
'success'
)
}
})
});
Solution
You have to store the delete url in a variable cause the 'this' doesn't refer anymore to the button
$('.deleteButton').on('click', function() {
event.preventDefault();
const deleteUrl = $(this).attr('href');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
setTimeout( function () {
$.get(deleteUrl,{});
}, 1200);
Swal.fire(
'Deleted!',
'',
'success'
)
}
})
Or do like this ,
setTimeout( function () {
$.get('{{ path('user_delete') }}',{});
}, 1200);
Answered By - Rova Ram
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.