[컴][php] lumen 에서 custom validator 만들기

custom validator / create custom validator / 루먼 커스텀 밸리데이터

lumen 에서 custom validator 만들기

'invest_amount' 라는 validator 에 대한 codes

class InvestItemController extends Controller{


    public function __construct(){
    }

    public function reserveInvest(Request $request){
        
        $this->validate($request, [
            'amount' => [
                'required',
                'numeric',
                'invest_amount',
            ],
        ]);
    }
    ...
}

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    ...

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
     public function boot()
     {
        ...

        Validator::extend('invest_amount', 'App\Rules\ValidInvestInput@passes');
        Validator::replacer('invest_amount', 'App\Rules\ValidInvestInput@message'); // error message
     }
}


<?php

namespace App\Rules;


class ValidInvestInput{

    public function __construct()
    {

    }

    /**
     * Determine if the validation rule passes.
     * 
     * @param  string  $attribute 'key' of request parameter
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value, $parameters)
    {

        return true;
    }
    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The given repository is invalid.';
    }
}

?>


References





  1. Custom Validation Rules- Lumen

댓글 없음:

댓글 쓰기