【发布时间】:2019-06-23 16:27:10
【问题描述】:
我在我的Lumen 应用程序中使用this 库来实现formRequest 功能。我已经按照文档中的说明进行了安装和配置,但似乎在某个地方我可能会丢失或在库中出现问题。以下是我的示例代码
验证类:
<?php
namespace App\Http\Requests;
use Pearl\RequestValidate\RequestAbstract;
use Illuminate\Validation\Rule;
use Illuminate\Http\Response;
class CommentFormRequest extends RequestAbstract
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required'
'title' => 'required|max:255',
'body' => 'required',
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\CommentFormRequest;
class CommentController extends Controller
{
public function add(CommentFormRequest $request)
{
$request->validated();
return response()->json(['Success']);
}
}
当我从邮递员提交无效数据时,会引发有效错误,但当我提交有效数据且未丢失任何表单字段时,则会显示以下错误。
(1/1) BadMethodCallException
Method App\Http\Requests\CommentFormRequest::validated does not exist.
以上错误完全让我感到困惑,因为如果找不到validated 方法,那么它如何为无效数据抛出错误?我曾尝试调试此问题,但由于我是 Lumen 的新手,所以我无法解决此问题。
【问题讨论】:
-
你的 Lumen 版本是多少?
-
@Mozammil 我正在使用流明 5.7.7
-
为什么 CommentFormRequest 类没有空格?
-
@SwaroopDeval 它在那里,现在更正了。
-
你在bootstrap/app.php中注册了服务提供者吗?