mawuekom/laravel-custom-form-request

Custom form request for laravel's projects provides with form data sanitization and api form request extension

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

This package is auto-updated.

Last update: 2024-05-25 04:23:06 UTC


README

Custom form request for laravel's projects provides with form data sanitization and api form request extension

Installation

You can install the package via composer:

composer require mawuekom/laravel-custom-form-request

Usage

If you want to sanitize your request data before validation, you can do this...
Check on Laravel Request Sanitizer for more informations

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
            ]
        ];
    }
}

You can also use form request for your Rest API.
Check on 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
            ]
        ];
    }
}

Hope this package will help you to build great things... 🏙️ Have fun 👍

License

The MIT License (MIT). Please see License File for more information.