Search code examples
phphtmlformsphalcon

Can I get an id of a submitted button in my Controller in Phalcon?


I'm looking for a way to get the id of a button when the button is submitted. Right now, when I submit the form and dump it in my controller, I can only access the value of the button, nothing else. I was wondering if there's a way to get the id as well. Here's my button in the volt file :

{{ submit_button("Forward", "name": "Forward", "class": "btn btn-primary-outline text-white text-sm", 'value':'submit', 'id' : 1) }}

I'm trying to check the request in the controller this way:

dd($this->request->getPost());

And this is the result :

Array (2) (
  [Forward] => String (33) "submit"
  [csrf] => String (32) "Y3BiVE9nRFBRWFZ3QTFsQkdoVjFHQT09"
)

It only gives me the value, nothing else. This is my Form :

class DashboardForm extends Form
{
    /**
     * @param null $entity
     * @param null $options
     */
    public function initialize($entity = null, $options = null)
    {
        $id = new Hidden('id');
        $this->add($id);
       
        // CSRF
        $csrf = new Hidden('csrf');
        $csrf->addValidator(new Identical([
            'value' => $this->security->getSessionToken(),
            'message' => 'CSRF validation failed'
        ]));
        $csrf->clear();
        $this->add($csrf);
    }
}

Solution

  • I can only access the value of the button, nothing else

    ...that's a feature of how HTML forms work. The value of the form element is submitted, using its name as the key. Nothing else is sent. There's nothing PHP/phalcon can do about that.

    As mentioned in another comment, if you need to submit additional data, consider using another (hidden) field in the HTML form to hold that data.