[英]Laravel 8 custom validation, get @error message in blade view
I have two inputs in a blade view from an array (each of them for diferent languages) and I build like this:我在一个数组的刀片视图中有两个输入(每个输入用于不同的语言),我这样构建:
@foreach ($langs as $lang)
<input
type="text"
class="form-control @error('title.{{$lang->isocode}}') is-invalid @enderror"
name="title[{{$lang->isocode}}]"
id="title[{{$lang->idioma->codi}}]"
value="{{ $lang->title }}"
>
@endforeach
My customForm Request Validation is this:我的自定义表单请求验证是这样的:
public function rules()
{
return [
'title.*' => ['required', 'string', 'max:100'],
];
}
public function messages()
{
return [
'title.*.required' => 'Title is required',
'title.*.string' => 'Title must be a string',
'title.*.max' => 'Title is too long',
];
}
and it works fine.它工作正常。 In fact, if I
@dump($errors)
, it shows:事实上,如果我
@dump($errors)
,它显示:
Illuminate\Support\ViewErrorBag {#353 ▼
#bags: array:1 [▼
"default" => Illuminate\Support\MessageBag {#354 ▼
#messages: array:1 [▼
"title.ca" => array:1 [▼
0 => "Title is too long"
]
]
#format: ":message"
}
]
}
But the problem is , if the validations fails, the .is-invalid
class is not applied in the input html tag.但问题是,如果验证失败,.is
.is-invalid
class不会应用于输入 html标记。 I think because @error('title.{{$lang->isocode}}')
is not correct.我认为是因为
@error('title.{{$lang->isocode}}')
不正确。
What I'm doing wrong?我做错了什么?
=>title field validation you can use in request method so remove this .{{$lang->isocode}} that is not correct.@error('title') print your error message.
class="form-control @error('title') is-invalid @enderror"
@error('title')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
i think the issue you did'nt provide index of error bag:我认为您没有提供错误包索引的问题:
@foreach ($langs as $k=>$lang)
<input
type="text"
class="form-control {{$errors->first('title.'.$k.'.'.$lang->isocode)?'is-invalid':''}}"
name="title[{{$lang->isocode}}]"
id="title[{{$lang->idioma->codi}}]"
value="{{ $lang->title }}"
>
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.