This package can use form request just as Laravel do.

Overview

Lumen Form Request

This package can use form request just as Laravel do.

Installation

Install by composer

    $ composer require chhw/form-request

In bootstrap/app.php, you should:

  1. uncomment $app->withFacades();
  2. add $app->register(CHHW\FormRequest\FormRequestServiceProvider::class);

Usage

Just like the way to use Laravel !

By structured file:

add something request like: app/Http/Requests/CreatePost.php,extends CHHW\FormRequest\FormRequest

app/Http/Requests/CreatePost.php

<?php

namespace App\Http\Requests;

use CHHW\FormRequest\FormRequest;

class CreatePost extends FormRequest
{
    /**
     * 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 [
            "title" => "required|string",
        ];
    }

    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
             if (true) {
                 $validator->errors()->add('field', 'Something is wrong with this field!');
             }
        });
    }
}

app/Http/Controllers/ExampleController.php:

use App\Http\Requests\CreatePost;

class ExampleController extends Controller
{
    public function test(CreatePost $request)
    {
        dd($request->all());
        // ...
    }
}

Or in controller:

app/Http/Controllers/ExampleController.php

use App\Rules\Uppercase;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    public function create(Request $request)
    {
        $request->validate(["author" => ["required", new Uppercase],]);
        // ...
    }
}

app/Rules/Uppercase.php

 <?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Uppercase implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return strtoupper($value) === $value;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be uppercase.';
    }
}

Get Request and Rule file by command:

php artisan make:request CreatePost
php artisan make:rule Uppercase

Customize Localization Error Messages

just like laravel !

add resources/lang/en/validation.php, and other country languages.

Error handler

app/Exceptions/Handler.php

public function render($request, Throwable $exception)
{
    if($exception instanceof ValidationException){
        return response()->json(["message" => $exception->getMessage(), "details" => $exception->errors()]);
    }
    return parent::render($request, $exception);
}
You might also like...
A package to validate email domains in a user registration form
A package to validate email domains in a user registration form

This package allows to define a subset of allowed email domains and validate any user registration form with a custom rule.

A package to validate email domains in a user registration form
A package to validate email domains in a user registration form

Laravel Email Domain Rule This package allows to define a subset of allowed email domains and validate any user registration form with a custom rule.

A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly.

PHPColors A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly. Requirements PHPColo

Make custom helper just with one command!

Laravel Custom Helper Make custom helper just with one command! Very light!!! Installation requires Laravel 8+ Via composer: $ composer require Ranjba

A Laravel package that allows you to use multiple
A Laravel package that allows you to use multiple ".env" files in a precedent manner. Use ".env" files per domain (multi-tentant)!

Laravel Multi ENVs Use multiple .envs files and have a chain of precedence for the environment variables in these different .envs files. Use the .env

 Laravel Logable is a simple way to log http request in your Laravel application.
Laravel Logable is a simple way to log http request in your Laravel application.

Laravel Logable is a simple way to log http request in your Laravel application. Requirements php = 7.4 Laravel version = 6.0 Installation composer

A super simple package allowing for use MySQL 'USE INDEX' and 'FORCE INDEX' statements.
A super simple package allowing for use MySQL 'USE INDEX' and 'FORCE INDEX' statements.

Laravel MySQL Use Index Scope A super simple package allowing for use MySQL USE INDEX and FORCE INDEX statements. Requirements PHP ^7.4 | ^8.0 Laravel

Easily capture every incoming request and the corresponding outgoing response in your Laravel app.
Easily capture every incoming request and the corresponding outgoing response in your Laravel app.

Easily capture every incoming request and the corresponding outgoing response in your Laravel app. This package is designed to work only with the Lara

Generate GitHub pull request preview deployments with Laravel Forge

Forge Previewer WARNING: This package is not ready for production usage. Use at your own risk. This package provides a forge-previewer command that ca

Owner
Keep Learning, and Keep Coding.
null
Aliyun oss filesystem storage adapter for laravel 5. You can use Aliyun OSS just like laravel Storage as usual

Aliyun oss filesystem storage adapter for laravel 5. You can use Aliyun OSS just like laravel Storage as usual

jacob 517 Dec 29, 2022
Composer package which adds support for HTML5 elements using Laravels Form interface (e.g. Form::date())

Laravel HTML 5 Inputs Composer package which adds support for HTML5 elements by extending Laravel's Form interface (e.g. Form::date()) Adds support fo

Small Dog Studios 11 Oct 13, 2020
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 622 Jan 2, 2023
Simple solution for form request validation on lumen.

Form Request Validation for Lumen This small package contains a laravel-like solution for request form validation. Contents Form Request Validation fo

Samuel Nunes de Souza 5 Nov 4, 2022
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Chat Create a Chat application for your multiple Models Table of Contents Click to expand Introduction Installation Usage Adding the ability to partic

Tinashe Musonza 931 Dec 24, 2022
Register for multiple Livestorm sessions from an external form. Practical use of Livestorm API with PHP/Javascript.

Livestorm Multi Session Registration Register for multiple Livestorm sessions from an external form. Practical use of Livestorm API with PHP/Javascrip

Nathan CHEVALIER 0 Dec 24, 2021
A demo of how to use filament/forms to build a user-facing Form Builder which stores fields in JSON.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Dan Harrin 41 Dec 24, 2022
laravel zibal - transaction request package for zibal

laravel zibal transaction request package for zibal Getting Started To get a local copy up and running follow these simple steps. Installation You can

Abbas mkhzomi 4 Jan 10, 2022
🖖Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.

Repository Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any

Awes.io 160 Dec 26, 2022
you can use this package with your develop operation....

Laravel Debugbar This is a package to integrate PHP Debug Bar with Laravel. It includes a ServiceProvider to register the debugbar and attach it to th

Eng Hasan Hajjar 2 Sep 30, 2022