我正在尝试在错误消息中获取自定义标签。我的输入是嵌套的,我可以使用点符号(例如 POLICY.HOLDER.NAME
)来验证它们。它们必须嵌套并且全部大写以匹配 db 结构,以便我可以轻松地对它们进行批量分配。
在 'attributes' => ['POLICY.HOLDER.NAME' => 'Policy Holder']
中使用相同的符号在 resources/lang/en/validation.php
没有结果。
我只是想让它们与我在表单中设置的标签相匹配。
app/Http/Controllers/OfferController.php(只是有趣的部分)
public function postOffer(OfferRequest $request) { //
$post_data = $request->all();
if (isset($post_data['POLICY'])) {
// code to get data from $_POST and assign to models
}
}
应用程序/Http/Requests/OfferRequest.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request, Auth;
class OfertaRequest extends Request
{
public function authorize() {
return Auth::check();
}
public function rules()
{
return [
'POLICY.HOLDER.NAME' => 'required',
];
}
public function forbiddenResponse()
{
return Response::make('Permission denied foo!', 403);
}
}
资源/lang/en/validation.php
'attributes' => [
'POLICY.HOLDER.NAME' => 'Some custom string here...',
],
如您所见,我尝试在自定义验证属性数组中添加输入名称但没有成功。这是我将输入留空时收到的错误消息:
The p o l i c y. h o l d e r. n a m e field is required.
注意空格。我也试过了,没用。
最佳答案
将其显式声明为嵌套数组:
'attributes' => [
'POLICY' => [
'HOLDER' => [
'NAME' => 'Some custom string here...',
]
]
],
关于validation - 如何在 Laravel 中为嵌套输入设置自定义属性标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32761230/