Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp Form helper PostLink edit
    text
    copied!<p>I'm using cakephp 2.</p> <p>Is it possible to use the form helper postLink function to update a record? Basically I want a "Approve" button that changes the "Approved" field of a record to 1.</p> <p>Everything I can find only relates to performing a delete function? The docs don't go into much details either.</p> <p>Any help would be great, thanks in advance</p> <p>My code:</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;postLink(__('Approve'), array( 'controller' =&gt; 'expenseclaims', 'action' =&gt; 'edit', $expenseClaim['ExpenseClaim']['id'], 'approved' =&gt; '1', 'approved_by' =&gt; $adminUser, ), array( 'class' =&gt; 'btn btn-danger' ), __('Are you sure you want to Approve # %s?',$expenseClaim['ExpenseClaim']['id'] )); ?&gt; </code></pre> <p>New code: view post link:</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;postLink(__('Approve'), array('action' =&gt; 'approve', $expenseClaim['ExpenseClaim']['id'], 'admin' =&gt; true), array('class' =&gt; 'btn btn-danger'), __('Are you sure you want to Approve # %s?',$expenseClaim['ExpenseClaim']['id'])); ?&gt; </code></pre> <p>Controller code:</p> <pre><code>public function admin_approve($id = null) { debug($this-&gt;request); $this-&gt;ExpenseClaim-&gt;id = $id; if (!$this-&gt;request-&gt;is('post') &amp;&amp; !$this-&gt;request-&gt;is('put')) { throw new MethodNotAllowedException(); } if (!$this-&gt;ExpenseClaim-&gt;exists()) { throw new NotFoundException(__('Invalid expense claim')); } if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { $this-&gt;request-&gt;data['ExpenseClaim']['approved'] = '1'; $this-&gt;request-&gt;data['ExpenseClaim']['approved_by'] = $this-&gt;Auth-&gt;user('id'); if ($this-&gt;ExpenseClaim-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash('The expense claim has been Approved', 'flash_success'); $this-&gt;redirect(array('action' =&gt; 'index', 'admin' =&gt; true)); } else { $this-&gt;Session-&gt;setFlash('The expense claim could not be approved. Please, try again.', 'flash_failure'); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload