DEV Community

Cover image for Customizing Values Validated by Laravel Form Requests
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Customizing Values Validated by Laravel Form Requests

This article was originally published on bmf-tech.com.

Overview

This post explains how to customize the values validated by Laravel form requests. It might be useful when you want to apply validation to route parameters, such as when the API endpoint is /post/:id/delete.

Modifying the validationData Method

We will modify the validationData method found in the Laravel API.

Here's an example of applying validation to the route parameter id.

<?php

namespace App\Http\Requests\Api\v1\Category;

use Illuminate\Foundation\Http\FormRequest;

class DeleteCategoryRequest extends FormRequest
{
    const NOT_FOUND_CODE = 400;

    /**
     * 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 [
            'id' => 'numeric',
        ];
    }

    /**
     * Get data to be validated from the request.
     *
     * @return array
     */
    protected function validationData()
    {
        return array_merge($this->request->all(), [
            'id' => $this->route('id'),
        ]);
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'id.numeric' => 'This id is not number',
        ];
    }

    /**
     * Get the proper failed validation response for the request.
     *
     * @param array $errors
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function response(array $errors)
    {
        $response['messages'] = $errors;

        return response()->json($response, (int) self::NOT_FOUND_CODE);
    }
}
Enter fullscreen mode Exit fullscreen mode

Thoughts

In the end, I haven't used this customization because it feels somewhat awkward.

References

Top comments (0)