我已经阅读了 12 次文档,并创建了以下内容:
class LSDLocationRequest extends FormRequest
{
/**
* 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 [
'organization_id' => 'required',
'name' => 'required|unique:lsd_locations'
];
}
public function messages()
{
return [
'name.unique:lsd_locations' => 'The LSD name is already used by a LSD Location.',
];
}
}
在控制器中,我这样做:
public function store(LSDLocationRequest $request, Organization $organization) {
$request->validated();
// ...
}
当我第一次以示例位置的名称提交此表单时,它可以工作。当我第二次使用相同的名称时,它显示给我:
这个名字已经被占用了。
我 110% 确定这是正确的控制器操作。为什么不显示我的自定义消息?我在表中的名称字段上有一个唯一键。它忽略了我的name.unique:lsd_locations
并编写了默认消息。
为什么它不使用我的消息?为什么使用默认消息?
只需将消息更改为:
name.unique
所以它应该是这样的:
public function messages()
{
return [
'name.unique' => 'The LSD name is already used by a LSD Location.',
];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句