【问题标题】:Laravel 5.2 custom validator not giving errorLaravel 5.2 自定义验证器没有给出错误
【发布时间】:2019-02-12 20:12:07
【问题描述】:

验证器失败时我没有收到任何错误。我有一个要验证请求 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】:

    integration_domain 应该是 input 字段名称,而不是规则,如果它是规则,那么您应该将它与 required 连接起来,如下所示:

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

    它对验证的基本理解。更多详情请查看here

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 2017-01-20
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 2023-04-03
      • 2016-08-06
      • 1970-01-01
      相关资源
      最近更新 更多