Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormHelper appends ID to explicitly defined URL #11794

Closed
PhantomWatson opened this issue Mar 6, 2018 · 6 comments
Closed

FormHelper appends ID to explicitly defined URL #11794

PhantomWatson opened this issue Mar 6, 2018 · 6 comments
Labels
Milestone

Comments

@PhantomWatson
Copy link
Contributor

PhantomWatson commented Mar 6, 2018

This is a: bug
CakePHP Version: Tested with 3.5.8, but the relevant code appears identical in the latest version (3.6.0-beta1)

What I did

On /users/my-account...

$this->Form->create(
    $user, 
    ['url' => ['controller' => 'Users', 'action' => 'myAccount']]
)

What happened

<form method="post" accept-charset="utf-8" action="/users/my-account/123"> <- ID appended

What I expected to happen

<form method="post" accept-charset="utf-8" action="/users/my-account">

Cause

FormHelper::_formUrl() modifies the value of $options['url'] by appending the current record's ID if $options['url']['_name'] is unspecified and no other numerically-indexed parameters are passed.

In an application I'm reviewing, this has the effect of the form on /users/my-account submitting to /users/my-account/$userId, despite explicitly setting the URL without an ID in $this->Form->create().

Recommendation

  • If it wouldn't be considered a breaking change, it would seem appropriate to remove the && isset($options['url']['_name'] from the condition before return $options['url'];, as requiring a route name seems kind of arbitrary.
  • Otherwise, a note in the FormHelper documentation about what specific circumstances result in IDs being appended to the URL that the form submits to would be helpful, maybe with an additional mention of workarounds, like simply wrapping the URL array in Router::url() so it gets passed as a string.
@ADmad
Copy link
Member

ADmad commented Mar 6, 2018

This unfortunately is expected behavior and there are tests for it.

But it is indeed wacky hidden automagic which should be changed in 3.6.

@ADmad ADmad added this to the 3.6.0 milestone Mar 6, 2018
@ADmad ADmad added the RFC label Mar 6, 2018
@markstory
Copy link
Member

But it is indeed wacky hidden automagic which should be changed in 3.6.

How do we go about removing the current behavior without breaking existing applications? Removing the id addition seems like the kind off thing that would break userland code.

@ADmad
Copy link
Member

ADmad commented Mar 6, 2018

Removing the id addition seems like the kind off thing that would break userland code.

Guess so, in 4.0 then.

@PhantomWatson
Copy link
Contributor Author

In the meantime, noting this behavior in the documentation might prevent some confusion for 3.x users. Perhaps just a "To prevent your entity's ID from being appended to the URL supplied $this->Form->create(), wrap URL arrays in Router::url()" or something.

@ADmad
Copy link
Member

ADmad commented Mar 6, 2018

@PhantomWatson Sounds good, can you please open a PR on the docs repo for the same?

@ADmad ADmad modified the milestones: 3.6.0, 4.0.0 Mar 6, 2018
PhantomWatson added a commit to BallStateCBER/commentaries-cake3 that referenced this issue Mar 6, 2018
Avoids this issue, which causes the user ID to be automatically
appended to the end of the URL, e.g. /users/my-account/123:
cakephp/cakephp#11794
@dereuromark
Copy link
Member

Added to roadmap as part of cleanup task: https://github.com/cakephp/cakephp/wiki/4.0-Roadmap#cleanup-tasks
We can close this then. Thank you for bringing this to our attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants