Reputation: 2434
I'm using cakephp 2.
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.
Everything I can find only relates to performing a delete function? The docs don't go into much details either.
Any help would be great, thanks in advance
My code:
<?php echo $this->Form->postLink(__('Approve'), array(
'controller' => 'expenseclaims',
'action' => 'edit', $expenseClaim['ExpenseClaim']['id'],
'approved' => '1',
'approved_by' => $adminUser,
), array(
'class' => 'btn btn-danger'
), __('Are you sure you want to Approve # %s?',$expenseClaim['ExpenseClaim']['id']
)); ?>
New code: view post link:
<?php echo $this->Form->postLink(__('Approve'), array('action' => 'approve', $expenseClaim['ExpenseClaim']['id'], 'admin' => true), array('class' => 'btn btn-danger'), __('Are you sure you want to Approve # %s?',$expenseClaim['ExpenseClaim']['id']));
?>
Controller code:
public function admin_approve($id = null) {
debug($this->request);
$this->ExpenseClaim->id = $id;
if (!$this->request->is('post') && !$this->request->is('put')) {
throw new MethodNotAllowedException();
}
if (!$this->ExpenseClaim->exists()) {
throw new NotFoundException(__('Invalid expense claim'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->request->data['ExpenseClaim']['approved'] = '1';
$this->request->data['ExpenseClaim']['approved_by'] = $this->Auth->user('id');
if ($this->ExpenseClaim->save($this->request->data)) {
$this->Session->setFlash('The expense claim has been Approved', 'flash_success');
$this->redirect(array('action' => 'index', 'admin' => true));
} else {
$this->Session->setFlash('The expense claim could not be approved. Please, try again.', 'flash_failure');
}
}
}
Upvotes: 4
Views: 8369
Reputation: 21743
yes, of course that is possible.
just post to something like
/expenseclaims/approve/id
to trigger the approve
action for example:
public function approve($id = null) {
if (!$this->request->is('post') && !$this->request->is('put')) {
throw new MethodNotAllowedException();
}
//validate/save
}
you can also make it more generic, of course
Upvotes: 3