验证器失败时我没有错误。我有一个功能,我想验证请求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回答。
integration_domain
应该是input
字段名称,而不是规则,如果它的规则,那么你应该连接required
像这样:
public function update(Request $request, RequestUser $user, $id)
{
$validatedData = $request->validate([
'input_variable_name' => 'required|integration_domain',
]);
//other stuff
}
它对验证的基本理解。有关详细信息,请参阅here