PHP code example of guirong / laravel-validate

1. Go to this page and download the library: Download guirong/laravel-validate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

guirong / laravel-validate example snippets



namespace App\Http\Requests;

use Guirong\Laravel\Validate\FormRequest;

class AddressRequest extends FormRequest
{
    /**
     * Auto validation
     *
     * @var boolean
     */
    protected $autoValidate = true;

    /**
     * Strictly verify scene
     * 自动校验时,严格验证场景`scene`是否存在;设置 false 表示场景不存在的情况下校验所有规则
     *
     * @var boolean
     */
    protected $sceneStrict = true;

    /**
     * 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' => ', 'city', 'county', 'address'
            ],

            // 地址编辑时,校验以下字段
            'update' => [
                'id', 'province', 'city', 'county', 'address'
            ],

            // 地址删除时,校验以下字段
            'delete' => [
                'id'
            ],
        ];
    }
}



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AddressController extends Controller
{
    
}


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AddressController extends Controller
{
    public function create(AddressRequest $request)
    {
        // ...校验通过后处理业务 
    }

}


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AddressController extends Controller
{
    public function edit(AddressRequest $request)
    {
        $request->validate('update');

        // 或者
        // $request->setScene('update')->validate();

        // ...校验通过后处理业务 
    }

}

namespace App\Http\Requests;

use Guirong\Laravel\Validate\FormRequest;

class AddressRequest extends FormRequest
{
    /**
     * Auto validation
     *
     * @var boolean
     */
    protected $autoValidate = false;

}


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AddressController extends Controller
{
    public function del(AddressRequest $request)
    {
        // ...校验通过后处理业务 
    }

}

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Route 路由自定义scene(场景方法)
        \Illuminate\Routing\Route::macro('scene', function ($scene = null) {
            $action = \Illuminate\Routing\Route::getAction();
            $action['_scene'] = $scene;
            \Illuminate\Routing\Route::setAction($action);
        });
    }

// 这里我们使用的验证场景是 delete
Route::post('/address/del', [\App\Http\Controllers\AddressController::class, 'del'])->scene('delete');



Route::post('/address/create', [\App\Http\Controllers\AddressController::class, 'create']);

Route::post('/address/edit', [\App\Http\Controllers\AddressController::class, 'edit']);

Route::post('/address/del', [\App\Http\Controllers\AddressController::class, 'del'])->scene('delete');




namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * 启动应用服务
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('cityEnable', function($attribute, $value, $parameters, $validator) {
            return $value == '杭州市';
        });
    }

    /**
     * 注册服务提供者
     *
     * @return void
     */
    public function register()
    {
        //
    }
}


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => '> '
            'id.y.


namespace App\Http\Requests;

use Guirong\Laravel\Validate\FormRequest;

class AddressRequest extends FormRequest
{
    //引入自定义扩展加载特性
    use \Guirong\Laravel\Validate\Traits\CustomExtensionsTrait; 

    // ..........省略代码..........


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'return [
            'id.$value, $parameters, $validator) {
            return $value == '杭州市';
        };
    }

}
bash
php artisan make:request AddressRequest
bash
php artisan make:controller AddressController