Laravel 5.2自定义验证器没有给出错误

问题描述 投票:1回答:1

验证器失败时我没有错误。我有一个功能,我想验证请求URL。

public function update(Request $request, RequestUser $user, $id)
    {
        $this->validate($request, [
            'integration_domain' => 'required',
        ]);
        //other stuff
    }

下面是我创建验证器的地方,

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Validator::extend('integration_domain', function($attribute, $value, $parameters, $validator) {
            if((strpos($parameters->input('url'), 'localhost') !== false) || 
                (strpos($parameters->input('url'), 'http://localhost') !== false) ||  
                (strpos($parameters->input('url'), 'https://localhost') !== false) ||  
                (strpos($parameters->input('url'), '127.0.0.1') !== false) ||
                (strpos($parameters->input('url'), 'http://127.0.0.1') !== false) ||
                (strpos($parameters->input('url'), 'http://127.0.0.1') !== false))
                return false;
            return true;
        });
    }
}

我跟着this回答。

laravel-5 laravel-5.2
1个回答
2
投票

integration_domain应该是input字段名称,而不是规则,如果它的规则,那么你应该连接required像这样:

public function update(Request $request, RequestUser $user, $id)
    {
        $validatedData = $request->validate([
            'input_variable_name' => 'required|integration_domain',
        ]);
        //other stuff
    }

它对验证的基本理解。有关详细信息,请参阅here

© www.soinside.com 2019 - 2024. All rights reserved.