Laravel - 唯一规则验证 - getMessages 不存在
Posted
技术标签:
【中文标题】Laravel - 唯一规则验证 - getMessages 不存在【英文标题】:Laravel - Unique rules validation - getMessages does not exist 【发布时间】:2016-08-03 05:16:51 【问题描述】:我正在编写一个简单的测验应用程序,其中包含一系列挑战(简单、中等、困难),每个挑战都有自己的问题。
要添加的问题应该是唯一的。
我有以下代码来“存储”数据
$v = ChallengeQuests::validate(Input::all());
if ( $v->passes() )
print 'validate passed';
$record = ChallengeQuests::create(array(
'challenge_id'=> (int) Input::get('challenge_id'),
'question_id'=> (int) Input::get('question_id')
));
$record->save();
return redirect()->to($url['redirects_to']);
else
print 'error';
print_r($v->getMessages());
return Redirect::to('/')->withErrors($v->getMessages());
在我的模型中,我有一个验证方法
// model
class ChallengeQuests extends Model
//
protected $table = 'challengequests';
protected $fillable=[
'challenge_id',
'question_id'
];
public static function validate($input)
$rules = array(
'challenge_id' => 'Required|Integer',
'question_id' => 'Required|Integer|Unique:questions,id'
);
return Validator::make($input, $rules);
但是当我运行我的代码时,Laravel 会抱怨
BadMethodCallException in Validator.php line 3016:
Method [getMessages] does not exist.
我想要它,以便 question_id 是唯一的。
我做错了什么?
编辑:
我正在使用:
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
堆栈跟踪:
BadMethodCallException in Validator.php line 3016:
Method [getMessages] does not exist.
in Validator.php line 3016
at Validator->__call('getMessages', array()) in ChallengeQuestionsController.php line 78
at ChallengeQuestionsController->store(object(Request))
at call_user_func_array(array(object(ChallengeQuestionsController), 'store'), array(object(Request))) in Controller.php line 80
at Controller->callAction('store', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ChallengeQuestionsController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ChallengeQuestionsController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ChallengeQuestionsController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\closure(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/Applications/XAMPP/xamppfiles/htdocs/laravel/scquiz/public/index.php') in server.php line 21
【问题讨论】:
请发布错误的整个堆栈跟踪。 按要求添加堆栈跟踪 【参考方案1】:用于获取验证消息的正确方法不是getMessages()
,而是messages()
,因此您的代码应如下所示:
return Redirect::to('/')->withErrors($v->messages());
此外,如果您使用的是 Laravel 5,您可能需要考虑使用 Form Request Validation,它与您尝试实现的功能相同,但方式更好,并在不同的层上处理验证,请注意为您传递错误和页面重定向。
通过在您的情况下使用表单请求,控制器方法将简化为:
public function store(ChallangeQuestsFormRequest $request)
ChallengeQuests::create($request->only('challenge_id', 'question_id'));
return redirect()->to($url['redirects_to']);
由于规则和验证以及发生错误时的重定向,将由ChallangeQuestsFormRequest
类处理。另外,使用create
创建模型入口会自动保存入口,所以create
的结果不需要使用save
。
【讨论】:
您的解决方案有效,我能够通过print_r
原始输出消息。我认为表单请求验证对我来说可能有点多,因为我还在学习 Laravel。我无法让页面呈现错误消息,但我想我可以通过 Flash 消息解决这个问题。
表单请求并不复杂,它们只是一个类,您将其作为参数注入控制器方法(如我的回答中所示),其中包含规则并为您处理验证。如果验证通过,则执行控制器方法代码,如果失败,它将重定向回您的表单,并将错误很好地添加到 $errors 变量中作为数组供您显示在视图中。如果您有时间,我建议您至少阅读我链接到的文档,因为它只有几段,您可能从一开始就掌握了它。
另外作为一个附注,Laravel 带有一个名为 dump
的好帮手函数,它类似于 print_r
,但它以正确的缩进和颜色很好地格式化输出。
感谢您的帮助以上是关于Laravel - 唯一规则验证 - getMessages 不存在的主要内容,如果未能解决你的问题,请参考以下文章