-1

Is it possible to add a class to the hidden form created by CakePHP's postLink form helper?

Here's my code


    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'
        ]) ?>

Note that I'm not looking to add a class to the link that gets created.

Any ideas are welcome!

ndm
  • 59,784
  • 9
  • 71
  • 110
Arnold
  • 69
  • 2
  • 10
  • It is possible, not overly straightforward though... why do you need to do that in the first place? Maybe there's a better way to do whatever you're trying to do. – ndm Jun 14 '17 at 12:54
  • I want to catch the form submission with JS client-side based on a class. – Arnold Jun 14 '17 at 13:16

1 Answers1

1

There's pretty much only one way currently, and that would be to change the formStart template temporarily, something along the lines of this:

// 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
]);

It's also possible to reset the templates to their default state using the resetTemplates() method, however this would reset all possible changes made to any of the templates, so it's probably better to play it safe as shown above.

See also

ndm
  • 59,784
  • 9
  • 71
  • 110