如何将 class 添加到 postLink 表单 - CakePHP 3.4

How to add a class to a postLink form - CakePHP 3.4

是否可以向 CakePHP 的 postLink 表单助手创建的隐藏表单添加 class?

这是我的代码


    Form->postLink(
        ' ' . __('Delete'),
        ['action' => 'delete', $this->fetch('item')],
        ['confirm' => __('Are you sure you want to delete # {0}?', $this->fetch('item'))
        ,'escape' => false
        ,'title' => __('Delete')
        ,'class' => 'btn btn-danger btn-xs isAction'
        ]) ?>

请注意,我不打算向创建的 link 添加 class。

欢迎提出任何想法!

目前几乎只有一种方法,那就是临时更改 formStart 模板,大致如下:

// read current template and set the new one
$formStart = $this->Form->getTemplates('formStart');
$this->Form->setTemplates([
    'formStart' => '<form class="hiddenFormClass"{{attrs}}>'
]);

echo $this->Form->postLink(/* ... */);

// set the template back to its previous state
$this->Form->setTemplates([
    'formStart' => $formStart
]);

也可以使用 resetTemplates() 方法将模板重置为默认状态,但是这会重置对任何模板所做的所有可能更改,因此最好按上面所示安全操作.

另见