Tuesday, September 28, 2010

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

I've submitted a patch to Lighthouse. Hopefully they adopt it into the
core. Meanwhile, I'm using my own patched version....

http://cakephp.lighthouseapp.com/projects/42648/tickets/1155

On Sep 28, 1:16 pm, DragonFlyEye <dragonflyey...@gmail.com> wrote:
> Ok, no one seems interested in biting on this, so I'll just go ahead
> and add my own information based on my own research in case someone
> else has the same problem:
>
> Looking at the helper code, it turns out that the array of options is
> stitched together with the array_merge() function:
>
> $actionDefaults = array(
>     'plugin' => $this->plugin,
>     'controller' => $view->viewPath,
>     'action' => $options['action'],
>     0 => $id
> );
> if (!empty($options['action']) && !isset($options['id'])) {
>     $options['id'] = $model .
> Inflector::camelize($options['action']) . 'Form';}
>
> $options['action'] = array_merge($actionDefaults, (array)
> $options['url']);
>
> The problem is that, according to the PHP.net website, array_merge()
> does not preserve numeric keys, which means that numerically-keyed
> array indexes will simply be appended to the end of the array, rather
> than overwriting existing values:
>
> <?php
> $arr1 = array('one' => 'one',
>     'two' => 'two',
>     0 => 0,
>     1 => 1);
> $arr2 = array('one' => 'uno',
>     'two' => 'dos',
>     0 => 'cero',
>     1 => 'uno');
>
> $result = array_merge($arr1, $arr2);
> ?>
> Returns:
> Array ( [one] => uno [two] => dos [0] => 0 [1] => 1 [2] => cero [3] =>
> uno )
>
> So the result is that my options array, which has an unindexed
> (therefore numerically indexed) value in it will not work once the
> Helper has inserted its own value. This is a pretty clear bug in the
> code, which sucks because as of this moment, I have no idea what to do
> to fix the issue apart from putting in a bug report. Which I guess I
> shall do now...
>
> On Sep 28, 10: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: