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

Set the pseudo method to "delete" for "Delete" links. #1028

Merged
merged 1 commit into from
Jan 17, 2025
Merged

Conversation

ADmad
Copy link
Member

@ADmad ADmad commented Jan 17, 2025

This sets the _method hidden field in the post link form to delete.

This sets the `_method` hidden field in the post link form to "delete".
@ADmad ADmad added this to the 3.x (CakePHP 5) milestone Jan 17, 2025
@ADmad ADmad changed the title Set pseudo method to "delete" for "Delete" links. Set the pseudo method to "delete" for "Delete" links. Jan 17, 2025
@ADmad
Copy link
Member Author

ADmad commented Jan 17, 2025

Should we target this to 3.next?

@LordSimal
Copy link
Contributor

if so, then we should change the generated delete method in the controller to only allow the DELETE HTTP Method as well.

@ADmad
Copy link
Member Author

ADmad commented Jan 17, 2025

It's very easy to miss specifying the method as evident here. I think it is quite likely users might be using their own templates and the baked controller code. So I would keep the controller template unchanged.

Allowing post in delete() won't cause any problem in majority of the apps. I made this change mainly so that new users can be made aware of the delete simulation.

@@ -65,7 +65,14 @@
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', {{ pk|raw }}]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', {{ pk|raw }}]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', {{ pk|raw }}], ['confirm' => __('Are you sure you want to delete # {0}?', {{ pk|raw }})]) ?>
<?= $this->Form->postLink(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldnt deleteLink() be cleaner to avoid the assoc array config overhead?

Copy link
Member

@dereuromark dereuromark Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Been using this for the last decade in my Form helper(s):

public function deleteLink(string|HtmlStringable $title, array|string|null $url = null, array $options = []): string {
	$options['method'] = 'delete';
	if (!isset($options['class'])) {
		$options['class'] = 'delete-link';
	}

	return $this->postLink($title, $url, $options);
}

I tend to avoid too much array config stuff in the templates themselves, and prefer speaking methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a deleteLink method. We could add one though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too was thinking of adding deleteLink().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -65,7 +65,14 @@
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', {{ pk|raw }}]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', {{ pk|raw }}]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', {{ pk|raw }}], ['confirm' => __('Are you sure you want to delete # {0}?', {{ pk|raw }})]) ?>
<?= $this->Form->postLink(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a deleteLink method. We could add one though.

@ADmad ADmad merged commit 13de03b into 3.x Jan 17, 2025
9 checks passed
@ADmad ADmad deleted the task/delete-link branch January 17, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants