Saturday, July 25, 2009

Re: Edit function not giving the results I expected.

I've got it working now, I had tried messing around with logic, but
didn't realise $this->data wasn't accessible in the controller until
the data had been saved. Was actually really simple in the end, I just
wasn't thinking about it in the right way!

Code is here for reference, it will redirect if the $id does not match
the $product array (which is taken from the requested $id):

$product = $this->Product->findById($id);

if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Product', true));
$this->redirect(array('controller' => 'products',
'action'=>'index'));
}

if (empty($product)) {
$this->Session->setFlash(__('Invalid Product', true));
$this->redirect(array('controller' => 'products',
'action'=>'index'));
}

On Jul 25, 6:51 pm, brian <bally.z...@gmail.com> wrote:
> Of course, there's going to be an $id set if you pass anything into
> the method. You'll need to add some logic to ensure that $id is an
> integer.
>
>
>
> On Sat, Jul 25, 2009 at 1:41 PM, number9<xpozit...@gmail.com> wrote:
>
> > I have just noticed something while working on an app, I use the
> > following code as part of an edit function (which I believe is the
> > "Cake" way:
>
> > if (!$id && empty($this->data)) {
> >        $this->Session->setFlash(__('Invalid Product', true));
> >        $this->redirect(array('controller' => 'products',
> > 'action'=>'index'));
> > }
>
> > I thought the result of this would be if a user trys to access /edit/
> > foo (foo does not exist) then they would be redirect and shown an
> > error message.
>
> > What actually happens, is that the form appears with no data in it and
> > the form actions to "/edit".
>
> > I have checked a few other cake apps and it appears to be the case
> > with them as well.
>
> > It is a minor point, but I'm just wondering if I have to add something
> > else, I'm guessing it is because /edit/$id serves as a catch all, and
> > technically an ID is set...
>
> > Any ideas?
>
> > Thanks in advance!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: