Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Do not use a Symfony Form to delete an article #427

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

Merged
merged 1 commit into from
Jan 12, 2017

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Jan 10, 2017

Because it's simpler without a SF Form

]);
}

/**
* Deletes a Post entity.
*
* @Route("/{id}", name="admin_post_delete")
* @Method("DELETE")
* @Method("POST")
Copy link
Contributor

Choose a reason for hiding this comment

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

@lyrixx, you could use _method field in the form instead of changing the required HTTP method to POST. http://symfony.com/doc/current/form/action_method.html

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 know, but I always found this totally useless ;)

Copy link
Member

Choose a reason for hiding this comment

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

Why don't you like the DELETE method? Is because browsers don't support it and we must simulate it with the _method trick? Thanks!

]);
}

/**
* Deletes a Post entity.
*
* @Route("/{id}", name="admin_post_delete")
* @Method("DELETE")
* @Method("POST")
Copy link
Member

Choose a reason for hiding this comment

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

if you change it to POST (which is something I do all the time to avoid having to enable the HttpMethodOverride which is disabled by default), I suggest changing the URL to /{id}/delete though

Copy link
Member

Choose a reason for hiding this comment

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

So you don't use the DELETE method either in your apps? Why don't you want to enable HttpMethodOverride? Security reasons? Performance reasons? Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

@stof good catch, I will change the URL
@javiereguiluz It just simulate a verb. But what does it bring? IMHO nothing except more code for no real value.

Copy link
Member

Choose a reason for hiding this comment

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

@javiereguiluz enabling it make it easier to create CSRF attacks (well, not for this action as it has a CSRF token). This is why it is disabled by default in Symfony.

and it brings no value (the browser is not doing a DELETE request) and can become a nightmare when dealing with cache proxies (because it is a POST request, and you cannot add a Vary on this override to use the right cache.

button_css: 'btn btn-lg btn-block btn-danger',
show_confirmation: true,
}, with_context = false) }}
{{ include('admin/blog/_delete_form.html.twig', { post: post}, with_context = false) }}
Copy link
Member

Choose a reason for hiding this comment

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

Missing space to end { post: post } or remove extra space to beginning {post: post} ? same in other files.

Copy link
Member

Choose a reason for hiding this comment

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

Nice catch! The style we use adds a space before and after the braces, so it should be { post: post }

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Fixed.

Because it simpler without a SF Form
$form = $this->createDeleteForm($post);
$form->handleRequest($request);
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
return $this->redirectToRoute('admin_post_index');
Copy link
Member

@yceruto yceruto Jan 10, 2017

Choose a reason for hiding this comment

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

The request could come from "edit page" or "show page", is right to redirect to index page? would be valid redirect to referer here?

In the other hand, could you add a flash messages (error) when this happens? Similar to:

The CSRF token is invalid. Please try to resubmit the form.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I keep the same behavior than before my changes...

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

👍

This looks much simpler to me.

@javiereguiluz
Copy link
Member

@lyrixx thanks for simplifying this feature!

@javiereguiluz javiereguiluz merged commit 9f8a40b into symfony:master Jan 12, 2017
javiereguiluz added a commit that referenced this pull request Jan 12, 2017
This PR was merged into the master branch.

Discussion
----------

Do not use a Symfony Form to delete an article

Because it's simpler without a SF Form

Commits
-------

9f8a40b Do not use a Symfony Form to delete an article
@elkuku elkuku mentioned this pull request May 27, 2017
javiereguiluz added a commit that referenced this pull request Feb 17, 2021
This PR was merged into the main branch.

Discussion
----------

Disable HTTP method override

We don't need this since the changes introduced in #427 and this goes in sync with the official Symfony recipe: symfony/recipes#892

Commits
-------

47eb3d9 Disable HTTP method override
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants