6

I've been trying to add a FormRequest with rules and message to my delete method, but the request is coming back empty and the rules are failing every time.

Is it possible to get the request data in a delete method?

Here's my request class:

use App\Http\Requests\Request;

class DeleteRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'staff_id' => ['required', 'exists:users,uid'],
            'reason' => ['required', 'string'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'staff_id.required' => staticText('errors.staff_id.required'),
            'staff_id.exists' => staticText('errors.staff_id.exists'),
            'reason.required' => staticText('errors.reason.required'),
            'reason.string' => staticText('errors.reason.string'),
        ];
    }
}

And the controller:

/**
 * Handle the 'code' delete request.
 *
 * @param integer $id            The id of the code to fetch.
 * @param DeleteRequest $request The request to handle the data.
 * @return response
 */
public function deleteCode($id, DeleteRequest $request)
{
    dd($request->all());
}
sepehr
  • 17,110
  • 7
  • 81
  • 119
  • Hello, show your code. Because is possible to get request data in any method. Maybe in delete method you don't past FormRequest Object as parameter. – Mauricio Wanderley Martins Nov 14 '17 at 21:01
  • Testing using Postman. It works if I pass the data as query parameters but NOT as the form body. –  Nov 14 '17 at 21:10
  • Maybe your DeleteRquest class needs to extend the FormRequest because this is the correct class to use the request form (used in view). When you are using the postman to test, try past values ​​in the form body configuration. Because this is the same form used in laravel views. – Mauricio Wanderley Martins Nov 14 '17 at 21:18
  • My custom Request class works perfectly for every other request, so I have no reason to believe it will fail with this one. And I have already tested with the default FormRequest class and the request data still doesn't exist. –  Nov 14 '17 at 21:20

1 Answers1

15

Even though the HTTP/1.1 spec does not explicitly state that DELETE requests should not have an entity body, some implementations completely ignore the body which contains your data, e.g. some versions of Jetty and Tomcat. On the other hand, some clients do not support sending it as well.

Think of it as a GET request. Have you seen any with form data? DELETE requests are almost the same.

You can read a LOT on the subject. Start here:
RESTful Alternatives to DELETE Request Body

It seems like that you want to alter the state of the resource rather than destroying it. Soft-deleting is not deleting and thus requires either a PUT or a PATCH method which both support entity bodies. If soft-deleting is not the case, you're doing two operations through one call.

sepehr
  • 17,110
  • 7
  • 81
  • 119
  • Thank you, I wasn't sure whether I was just missing a certain Laravel "quirk" that meant the data wasn't being sent through properly. Your explanation clarifies things a lot for me. –  Nov 15 '17 at 08:44