Laravel 5.3动态地将验证器绑定到自定义表单请求

时间:2018-01-11 20:38:58

标签: php laravel validation

我正在使用Laravel 5.3开发一个系统,我正在尝试更新自定义验证规则。该架构目前如下:

ProfileStoreRequest - > ProfileValidator ClientStoreRequest - > ClientValidator ...

我在这里基本上要做的是只有一个名为“StoreRequest”的对象,它将根据被调用的路由调用正确的验证器。这是我到目前为止所做的:

在我的路线中:

Route::group([
    'prefix' => 'manage',
    'namespace' => 'Manage',
    'validators' => [
        'manage.profile.storeAjax' => [
            'name' => "required|max:40",
            'color' => "integer|digits_between:0,7",
            'service' => "integer", //digits_between:3,10
            'company_id' => "required|integer|exists:companies,id,deleted_at,NULL",
            'site_id' => "integer|siteIdExists"
        ]
    ]], function () {

    Route::post('/site/storeAjax', 'SiteController@storeAjax')->name('manage.site.storeAjax');
    Route::post('/company/storeAjax', 'CompanyController@storeAjax')->name('manage.company.storeAjax');

    Route::post('/employee/store', 'EmployeeController@store')->name('manage.employee.store');
    Route::post('/employee/addProfile', 'EmployeeController@addProfile')->name('manage.employee.addProfile');
    Route::post('/employee/removeProfile', 'EmployeeController@removeProfile')->name('manage.employee.removeProfile');
    Route::post('/employee/addSite', 'EmployeeController@addSite')->name('manage.employee.addSite');
    Route::post('/employee/removeSite', 'EmployeeController@removeSite')->name('manage.employee.removeSite');

    Route::post('/message/storeAjax', 'MessageController@storeAjax')->name('manage.message.storeAjax');
    Route::post('/profile/storeAjax', 'ProfileController@storeAjax')->name('manage.profile.storeAjax');
    Route::post('/timeEntry/storeAjax', 'TimeEntryController@storeAjax')->name('manage.timeEntry.storeAjax');
});

接下来是我的StoreRequest:

namespace App\Http\Requests;

use App\Http\Validators\ProfileValidator;
use Auth;
//use App\Model\TimeEntry;
use DateUtil;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;

class StoreRequest extends AbstractRequest {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() {
//        prj(__METHOD__);

        $authorized = parent::authorize();

        if ($authorized) {
            $user = Auth::user();
            if ($user && $user->can('write')) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() {
        parent::rules();

        return $this->route()->getAction()['validators'][$this->route()->getName()];

    }

    /**
     * User messages
     * 
     * @return array
     */
    public function messages() {
        $messages = array_merge(parent::messages(), [
            'exists' => 'The selected :attribute is invalid for this time entry id.'
        ]);


        return $messages;
    }

    public function validate()
    {
        parent::validate();
    }
}

当然,我的所有自定义验证器都是使用服务提供商注册的(这是一个带有profileValidator的示例):

profileServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Validator;

class ProfileServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot() {
        Validator::extend('siteIdExists', 'App\Http\Validators\ProfileValidator@validateSiteIdExists');
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register() {
        //
    }

}

ProfileValidator.php

namespace App\Http\Validators;

use App\Model\Site;
use Mockery\Exception;


class ProfileValidator
{
    public function validateSiteIdExists($attribute, $value, $parameters, $validator)
    {
        if ($value == -1)
            return true;
        else
        {
                return Site::where(
                    [
                        ['id', '=', $value],
                        ['company_id', '=', $validator->getData()['company_id']]
                    ]
                )->whereNull('deleted_at')->exists();

        }
    }
}

基本上,我的StoreRequest能够从路由中加载其验证规则......但是,无论我怎么努力,我都无法想出如何绑定我想要的验证器。任何人都可以帮助我吗?

0 个答案:

没有答案