mawuekom/laravel-custom-form-request

为laravel的项目提供自定义表单请求,包括表单数据清理和API表单请求扩展

v1.0.0 2021-06-24 21:52 UTC

This package is auto-updated.

Last update: 2024-09-25 04:59:12 UTC


README

为laravel的项目提供自定义表单请求,包括表单数据清理和API表单请求扩展

安装

您可以通过composer安装此包

composer require mawuekom/laravel-custom-form-request

使用

如果您想在验证之前清理请求数据,可以这样做...
请查阅Laravel Request Sanitizer获取更多信息

namespace App\Http\Requests;

use Mawuekom\CustomFormRequest\Requests\SanitizeFormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            'name'          => 'required|string|max:255',
            'first_name'    => 'required|string|max:255',
            'email'         => 'required|string|email|max:255|unique:users',
            'password'      => 'required|string|min:6|confirmed',
        ];
    }

    /**
     * Get sanitizers defined for form input
     *
     * @return array
     */
    public function sanitizers(): array
    {
        return [
            'name' => [
                Capitalize::class,
            ],
            'first_name' => [
                CapitalizeEachWords::class
            ]
        ];
    }
}

您还可以将表单请求用于您的REST API。
请查阅API Form Request

namespace App\Http\Requests;

use Mawuekom\CustomFormRequest\Requests\SanitizeApiFormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            'name'          => 'required|string|max:255',
            'first_name'    => 'required|string|max:255',
            'email'         => 'required|string|email|max:255|unique:users',
            'password'      => 'required|string|min:6|confirmed',
        ];
    }

    /**
     * Get sanitizers defined for form input
     *
     * @return array
     */
    public function sanitizers(): array
    {
        return [
            'name' => [
                Capitalize::class,
            ],
            'first_name' => [
                CapitalizeEachWords::class
            ]
        ];
    }
}

希望这个包能帮助您构建伟大的事物... 🏙️ 祝您玩得开心 👍

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。