Tuesday, September 28, 2010

Re: Form Helper Automatically Inserting the ID of the Node?

This happens when you have $this->data['ModelAlias']['id'] set in your
$this->data. If the id is present the helper assumes you're
preforming an edit and as such appends the ID to the url as that is
the default baked behavior (to receive an ID on edit/delete). To
turn this off, make sure to unset $this->data['ModelAlias']['id'] in
your controller if you get a fail.

Example:

function some_action(){
if(!empty($this->data)){
if($this->Model->save($this->data)){
//Hurray, do something like redirect.
}
else{
/*Boo, we had a validation error, unset the id so the form-
>create() doesn't append it to the url, we'll manually add it via a
hidden field instead.*/
unset($this->data['Model']['id']);
}
}
}

I've had to use this trick many times as I don't like passing IDs in
the URL, I prefer to POST them in the data array, but if there is a
validation error all of a sudden the form helper wants to "Help" and
add the id onto the end of the action url.

I hope this helps,
Nick

On Sep 28, 8:09 am, DragonFlyEye <dragonflyey...@gmail.com> wrote:
> Ok, I don't understand this. I'm creating a form which for reasons I
> won't go into displays on a page with human-friendly URLs. So, I don't
> want to have to use a URL that includes a numeric ID for the database
> node I'm working on. I've run into two problems I find exceptionally
> odd:
>
> 1. When creating the action for a form using the "action" element of
> the options array, there is no way to specify that the URL should
> include the identifier of the node you're working on. It's quite
> common to have a url like /stuff/display/thisThing and update the
> values for that node. Why does CakePHP not allow us to specify an
> action like /display/thisThing?
>
> 2. Since that doesn't work, I'm using the 'url' element of the options
> array. Implicitly, if I'm creating my own URL from whole cloth, I
> certainly already know what I want that URL to be. But the Cake form
> helper takes it upon itself to prepend the form with ID of the node.
>
> By way of example: I am creating the form using the following code:
>
> <?php echo $form->create(null, array('url'        => array('controller'        =>
> 'charts',
>                                                                                       'action'  => 'display',
>                                                                                       $chart['Chart']['chartid']))); ?>
>
> On first page load, the output form looks fine:
> <form id="ChartAdminDisplayForm" method="post" action="/admin/charts/
> display/BUD" accept-charset="iso-8859-1">
>
> But after hitting the submit button, I get the following:
> <form id="ChartAdminDisplayForm" method="post" action="/admin/charts/
> display/8/BUD" accept-charset="iso-8859-1">
>
> Can anybody tell me why this happens and if there is a way around it?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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: