martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
415 stars 22 forks source link

Implement `MethodOverride` Middleware for HTTP Method Overrides in Forms #215

Closed treagod closed 5 months ago

treagod commented 6 months ago

Description

HTML forms natively support only GET and POST requests. To enable RESTful actions like DELETE, PUT, and PATCH, we need a mechanism to translate form submissions into their intended HTTP methods. This would help to resolve #184.

Proposal

Introduce a MethodOverride middleware that intercepts form-data POST requests. This middleware should:

Example Usage:

<form method="POST">
  <input type="hidden" name="_method" value="DELETE">
</form>

Considerations

Implement tag helper, e.g. {% form_delete %}, to help reduce the chance of typos being made.

ellmetha commented 6 months ago

I think that a method_input template tag would be more consistent with other template tags that generate form inputs (such as the csrf_input one). For example:

{% method_input "DELETE" %}