3

I have created a custom component with form to update prices of four product to be displayed on frontend.

My main controller code is here:

public function display($cachable = false, $urlparams = false) {

    require_once JPATH_COMPONENT.'/helpers/calculator.php';

    $view       = JFactory::getApplication()->input->getCmd('view', 'pricetable');
    $layout     = JFactory::getApplication()->input->getCmd('layout', 'edit');

    JFactory::getApplication()->input->set( 'layout', $layout );
    JFactory::getApplication()->input->set('view', $view);
    JFactory::getApplication()->input->set('id', 1);

    parent::display($cachable, $urlparams);
    return $this;
}

id is set to 1 so it loads only first row from database.

code for pricetable container is:

function __construct() {
    $this->view_list = 'pricetable';
    parent::__construct();
}

Now in admin backend the form is loaded as desired with the first row of data. When I try to save the form it is redirected to administrator/index.php?option=com_calculator&view=pricetable and error is:

Error: You are not permitted to use that link to directly access that page (#1).

my form action is:

<?php echo JRoute::_('index.php?option=com_calculator&task=pricetable.edit&id='.(int) $this->item->id); ?>

Please suggest where I am doing wrong. It is third day I'm scratching my head. :(

Harpreet
  • 709
  • 5
  • 14
  • and if you set form action as `item->id); ?>`? – Marko D Apr 05 '13 at 09:11
  • Nothing happens with this as well. What I think is the problem with pricetable controller. Isn't there any way to redirect component to `administrator/index.php?option=com_calculator&task=pricetable.edit&id=1` on its first load. – Harpreet Apr 05 '13 at 09:18
  • 1
    i think u can update data on same page u are. nowhere else. so just make this field empty (`action`) to make sure u're staying on same page... – StasGrin Apr 05 '13 at 10:14
  • @StasGrin keeping action empty saves the form data to new instance, but do not update the existing one. – Harpreet Apr 05 '13 at 10:44
  • 1
    that's why? u can do updating actions (or calling them) inside your code whenever it is. New instance or update new - add one more `IF` and `hidden` input with for example `name=task value=update` – StasGrin Apr 05 '13 at 10:48
  • Using empty `action=""` and adding a hidden field `` worked. You saved my life @StasGrin. Thank you. – Harpreet Apr 05 '13 at 11:20
  • oh.. that was so obvious, that i don't believe it's solution. posted as answer for closing this qestion. – StasGrin Apr 05 '13 at 11:22

1 Answers1

4

You can do updating actions (or calling them) inside your code whenever it is. New instance or update new - just add one more if in code and hidden input on form. For example:

<input type="hidden" name="task" value="update" />

StasGrin
  • 1,800
  • 2
  • 14
  • 30