Cake 4: Check for button click in controller

In Cake 2.x, I could simply do if (isset($this->request->data['button_name'])) if I wanted to verify that a certain form button was clicked. What’s the Cake 4 equivalent if I have a button configured like this?

<?= $this->Form->button('Export to Excel', ['type' => 'submit', 'name' => 'export', 'id' => 'export']) ?>

please try this:

$button = $this->request->getData('export');
          if (isset($button)) {
            //your code
          }

No joy, alas. Debug of $button upon submit shows null, so it looks like the value isn’t getting to my export() method.

Further background: I have three buttons on this particular form : Search, Reset, and Export to Excel. The third one lets the user export the result set from the search after Search is clicked. The search() method passes $query results to export(), which I want to trigger with Export to Excel. In Cake 2.x, this was a cinch. I did something similar to what you suggested.

For some reason, export() isn’t recognizing the button click.

I have in my form:

        <?= $this->Form->button(__('Submit'),['name'=>'export']) ?>
        <?= $this->Form->button(__('Submit'),['name'=>'search']) ?>
        <?= $this->Form->button(__('Submit'),['name'=>'reset']) ?>

and can catch each button in my controller

 if ($this->request->is(['patch', 'post', 'put'])) {     
     $export = $this->request->getData('export');
      if (isset($export)) {
        debug($this->request->getData());
      }
      $search = $this->request->getData('search');
      if (isset($search)) {
        debug($this->request->getData());
      }
      $reset = $this->request->getData('reset');
      if (isset($reset)) {
        debug($this->request->getData());
      }

//Rest of Code
}

and it works

Interesting. What are you getting back when you debug? When I debug any button in either search() or export(), I get back null. The Export button also just resubmits my search form instead of triggering export(). In my Cake 2.x app, I handled that in my search() method by checking for which button was clicked and just didn’t render a view if it was Export. Here, I can’t even get the buttons to produce a value.

This is the

debug($this->request->getData())

from export button:

> 'name' => 'Ranker und Schlanker', 'secondname' => '', 'street' => 'Wiesenst. 42', 'zipcode' => '57612', 'town' => 'Giesenhausen', 'phone' => '564654', 'fax' => '', 'email' => 'info1@giesenhausener.de', 'url' => '', 'vatidnumber' => 'sfds', 'taxnumber' => 'sdfsfd', 'businessregistration' => '', 'country_id' => '1', 'state_id' => '3', 'membership_id' => '1', 'export' => '',

This is the

debug($this->request->getData())

from search button:

‘name’ => ‘Ranker und Schlanker’, ‘secondname’ => ‘’, ‘street’ => ‘Wiesenst. 42’, ‘zipcode’ => ‘57612’, ‘town’ => ‘Giesenhausen’, ‘phone’ => ‘564654’, ‘fax’ => ‘’, ‘email’ => ‘info1@giesenhausener.de’, ‘url’ => ‘’, ‘vatidnumber’ => ‘sfds’, ‘taxnumber’ => ‘sdfsfd’, ‘businessregistration’ => ‘’, ‘country_id’ => ‘1’, ‘state_id’ => ‘3’, ‘membership_id’ => ‘1’, ‘search’ => ‘’,

Other question:
Wouldn’t it make sense to use postlinks

for example:
<?= $this->Form->postLink(__('Export'), ['action' => 'export', $entity->id], ['confirm' => __('Are you sure you want to export # {0}?', $entity->id)]) ?>

I could use a postlink, but I can’t pass an array to it, as far as I know, and I need the full $query array for the export to work.

Your previous post did put me on to something, though. In my app, since I’m using a search plugin, I use $this->request->getQueryParams() to read the form data instead of $this->request->getData(), so that would explain why the latter always comes up null in a debug (I’m spitballing there, but debug of getQueryParams() works, whereas debug of getData() does not). Neither shows that my button click evaluates to anything, though.

Just to clarify:
If you hit i.e. the “export Button” and then in your controller do

debug($this->request->getData('export);

the result will be NULL, because the key “export” has empty value.
So you have to go this way, which checks, if there is a key named “export”:

$export = $this->request->getData('export');
      if (isset($export)) {
        debug($this->request->getData());
      }

Yep, tried that in both my search() and export() methods, to the letter. Doesn’t work in either case. App just isn’t recognizing any button click as input.

One last idea:
3 Buttons in your Form:

    <?= $this->Form->button(__('export'),['name'=>'action','value'=>'export']) ?>
    <?= $this->Form->button(__('search'),['name'=>'action','value'=>'search']) ?>
    <?= $this->Form->button(__('reset'),['name'=>'action','value'=>'reset']) ?>

And in the Controller:

  $action = $this->request->getData('action');
  debug($action);

You will have to use a hidden hield and some javascript,

If the export button is clicked fill the hidden field with a value (export), and then submit the form.

Check in the controller the value of the hidden field.