php - CakePHP 3.0 Confirm Delete Modal in Bake Template

570

I am trying to make a bake theme that integrates twitter bootstrap. And uses the bootstrap modal dialog to confirm deletion of data.

I am following this link here and also using CakePHP 3 Bootstrap Helpers.

I am creating the theme in myplugins/themename/src/Template/Bake/Template/index.ctp &view.ctp

Here is my code for the delete button inindex.ctp

<?= $this->html->link(__($this->Html->faIcon('close').' Delete'),
['action' => '#'],
['escape' => false,
'class' => 'btn btn-sm btn-danger btn-confirm',
'data-toggle'=> 'modal',
'data-target' => '#ConfirmDelete',
'data-action'=> Router::url(['delete', <%= $pk %>]),
false]) ?>

Getting the error:

Error: Class 'Router' not found

when viewing the /bookmarks index page

I have useCake\Routing\Router; at the top of myindex.ctp

Then myview.ctp

<?= $this->Html->link(__($this->Html->faIcon('close')),
['action' => '#'],
['escape' => false,
'class' => 'btn btn-sm btn-danger btn-confirm',
'data-toggle'=> 'modal',
'data-target' => '#ConfirmDelete',
'data-action'=> ['/<%= $details['controller'] %>/delete/'.<%= $otherPk %>],
false]) %>

This seems to be working, but the resulting data-action is/Tags/delete/1 with a capital on the T - not sure if I will run into issues down the road?

Everything else is working, except for the Router::url in theindex.ctp

Modal Code inplugins/themename/src/Template/Layout.default.ctp

<?php
$content = 'Are you sure you want to delete this element?';
echo $this->Modal->create(['id' => 'ConfirmDelete']) ;
echo $this->Modal->header('Confirm Delete', ['close' => false]) ;
echo $this->Modal->body($content, ['class' => 'my-body-class']) ;
echo $this->Modal->footer([
    $this->Form->button('Close', ['data-dismiss' => 'modal']),
    $this->Form->postLink(__('Delete'), ['action' => 'delete'],         
['class' => 'btn btn-danger'], false)]) ;
echo $this->Modal->end() ;
?>

jQuery code to change the postLink action in the modal

jQuery.noConflict();

(function($) {
    $(document).ready(function () {
        $(".btn-confirm").on("click", function () {
            var action = $(this).attr('data-action');
            $("#ConfirmDelete form").attr('action', action);
        });
    })
})(jQuery);
196

Answer

Solution:

This seems to work, unless this is not the true "CakePHP Way".

Inindex.ctp

ReplacingRouter::url() with$this->request->here

<?= $this->html->link(__($this->Html->faIcon('close')),
['action' => '#'],
['escape' => false,
'class' => 'btn btn-danger btn-confirm',
'data-toggle'=> 'modal',
'data-target' => '#ConfirmDelete',
'data-action'=> $this->request->here.'/delete/'.<%= $pk %>,
 false]) ?>

Inview.ctp

Wrapping<%= $details['controller'] %> instrtolower()

<?= $this->Html->link(__($this->Html->faIcon('close')),
['action' => '#'],
['escape' => false,
'class' => 'btn btn-danger btn-confirm',
'data-toggle'=> 'modal',
'data-target' => '#ConfirmDelete',
'data-action'=> '/'.strtolower('<%= $details['controller'] %>').'/delete/'.<%= $otherPk %>,
false]) %>

Any input from more advanced Cake users?

People are also looking for solutions to the problem: PHP SQL server extensions broken

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.