CakePHP Getting Back to Where You Were After You Submit an Edit Form

Written by James McDonald

April 3, 2015

I don’t know if this is the proper way to do it but…

You have an edit button in a view pointing to a form…

When you click edit and you navigate to the edit page and you click submit. The edit actions default is to take you back to the controllers index action which may not have been where you were which confuses the user.

So to do the page ==> edit ==> back to the page you were on round trip you need to do the following

In the controllers edit action populate $this->request->data[‘Model’][‘referer’] with the address of the calling page when you intially request the edit form page

Store that in the edit form as a hidden field

When you post the form back pick up the value as the redirect value instead of the default ‘index’ action.

# Controller action

public function edit_pallet($id = null) {
       
        if (!$this->Label->exists($id)) {
            throw new NotFoundException(__('Invalid label'));
        }
        if ($this->request->is(array('post', 'put'))) {
            // store the value the field was before changed
         
            if ($this->Label->save($this->request->data)) {
                $this->Session->setFlash(__('The label has been saved.'));
                # this is added to pull the redirect location from the
                # submitted form
                return $this->redirect($this->request->data['Label']['referer']);
            } else {
                $this->Session->setFlash(__('The label could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Label.' . $this->Label->primaryKey => $id));
            
            $label_info = $this->Label->find('first', $options);
            $label_info['Label']['qty_before'] = $label_info['Label']['qty'];
            $this->request->data = $label_info;
        }
	$inventory_statuses = $this->Label->InventoryStatus->find('list');
        $locations = $this->Label->Location->find('list');
        $shipments = $this->Label->Shipment->find('list');

        # store the page that the user clicked the edit button of
        $this->request->data['Label']['referer'] = $this->referer();
        $this->set(compact('locations', 'shipments',  'inventory_statuses'));
    }


# View 

<div class="labels form">
< ?php echo $this->Form->create('Label'); ?>
	<fieldset>
		<legend>< ?php echo __('Edit'); ?></legend>
	< ?php
		echo $this->Form->hidden('id');
                # add a hidden field to store the referer address in
                echo $this->Form->hidden('referer');
		echo $this->Form->input('item', array('disabled' => 'disabled'));
		echo $this->Form->input('description', array('disabled' => 'disabled'));
                echo $this->Form->input('pl_ref' , array('disabled' => 'disabled'));
		echo $this->Form->input('best_before',array('disabled' => 'disabled'));
		echo $this->Form->hidden('gtin14');
		echo $this->Form->input('qty');
		echo $this->Form->input('inventory_status_id', array('options'=> $inventory_statuses, 'empty' => array(0 => '(no status)'))    );

		echo $this->Form->hidden('qty_before');
                echo $this->Form->hidden('qty_previous');
		echo $this->Form->hidden('sscc');
		echo $this->Form->hidden('batch');
		echo $this->Form->input('location_id');
		echo $this->Form->hidden('shipment_id');
	?>
	</fieldset>
< ?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
	<h3>< ?php echo __('Actions'); ?></h3>
	<ul>
<li>< ?php echo $this->Html->link(__('Main Menu'), array('controller' => 'pages', 'action' => 'index'), array('title' => "Go to main menu")); ?></li>
		<!-- <li>< ?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Label.id')), array(), __('Are you sure you want to delete # %s?', $this->Form->value('Label.id'))); ?> -->
		<li>< ?php echo $this->Html->link(__('List Labels'), array('action' => 'index')); ?></li>
		<li>< ?php echo $this->Html->link(__('List Locations'), array('controller' => 'locations', 'action' => 'index')); ?> </li>
		<li>< ?php echo $this->Html->link(__('New Location'), array('controller' => 'locations', 'action' => 'add')); ?> </li>
		<li>< ?php echo $this->Html->link(__('List Shipments'), array('controller' => 'shipments', 'action' => 'index')); ?> </li>
		<li>< ?php echo $this->Html->link(__('New Shipment'), array('controller' => 'shipments', 'action' => 'add')); ?> </li>
	</ul>
</div>

2 Comments

  1. Eduardo

    Saved my life!
    It works fine and full solution available!

    Reply
    • James

      Glad it helped!

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...