我一直在尝试为laravel控制器中的字段验证设置自定义错误消息。
我已经设置了自定义消息,但我不知道如何在现场工作中创建Rule:。
我正在粘贴下面的控制器。
public function registerNewAnkda(Request $request)
{
$attr = $request->validate([
'game_id' => ['required','numeric'],
'market' => ['required',Rule::in(['Kalyan', 'Time','Ratan','Main','Milan'])],
'DoN' => ['required',Rule::in(['Day', 'Night'])],
'OoB' => ['required',Rule::in(['Open', 'Bandh','Jodka'])],
'type' => ['required',Rule::in(['Ankda'])],
'selection' => ['required','numeric',Rule::in(['0', '1','2','3','4','5','6','7','8','9'])],
'stake_amt' => ['required','numeric'],
'to_win_amt' => ['required','numeric'],
],
//Below code is used to display custom validation error messages!
[ 'game_id.numeric' => 'The :attribute should be Unique and Numeric ! MSG FOR PINKA !',
'selection' => 'Rule::in should be Single Number ! E.G - 0,1,2,3,4,5,6,7,8,9'
]
);
字段中的规则:自定义消息未显示!所以我们非常感谢您的帮助!
试试这个:
if($attr->fails()) {
return Redirect::back()->withErrors($attr);
}
一次显示所有错误
@if($errors->any())
{!! implode('', $errors->all('<div>:message</div>')) !!}
@endif
在每个字段下显示错误。
@error('firstname')
<div class="error">{{ $message }}</div>
@enderror