Hooman Ahmadi
Hooman Ahmadi

Reputation: 1397

Submit Array with Cakephp Form Helper

I have the below formhelper code where $spot is an array of variables:

   echo $form->create('Spot', array('controller' => 'spots', 'action' => 'view'));
                    echo $form->hidden('spotdata', array('value' => $spot));
                    echo $form->end('View');

When I print_r($this->data) in my controller, the spotdata is empty. Can the formhelper accept values that are arrays? Is there any way to do this? Please let me know, thanks!

Upvotes: 0

Views: 1257

Answers (3)

contrebis
contrebis

Reputation: 1318

I notice you are sending the data to a view. It's more idiomatic to just send the ID to the view, and the data can be reloaded from the database. In which case a link would be enough:

echo $this->Html->link('View', 
    array('controller' => 'spots', 'action' => 'view', $spot['Spot']['id']));

If you are trying to keep the state between pages you may find it easier and more secure to use the SessionComponent to do this (never trust data sent back from the client). In your controller method, this is as easy as:

$this->Session->write('Spot.spotData', $spot);

and

$spot = $this->Session->read('Spot.spotData');

to read back the data.

Upvotes: 1

Arjen
Arjen

Reputation: 1321

Instead of looping through the array and echo'ing each key, you can also serialize() the array and place it in a single hidden input. Afterwards you can deserialize() it

Upvotes: 0

Scott Harwell
Scott Harwell

Reputation: 7465

No, since it echoes an HTML input tag. The value has to be a string or something that can be cast as a string. View your HTML source.

Upvotes: 1

Related Questions