这是我的html文件的一部分。
<div class="form-group{{ $errors->has('address_name') ? ' has-error' : '' }}">
<label for="address_name">{{ trans('address.address_name') }} <span class="required_field">*</span></label>
<input name="address_name" type="text" class="form-control" id="address_name" placeholder="{{ trans('address.address_name_placeholder') }}" maxlength="30">
@if($errors->has('address_name'))
<span class="help-block">{{ $errors->first('address_name') }}</span>
@endif
</div>
我需要在Laravel 5.1中处理Ajax请求的错误。以下是我用于处理错误的代码:
$validator = Validator::make($addressData, $this->rules());
if ($validator->fails())
{
return response()->json([
'success' => 'false',
'errors' => $validator->errors()->all(),
], 400);
}
else
{
//Save Address
try
{
$this->insertAddress($addressData);
return response()->json(['success' => true], 200);
}
catch(Exception $e)
{
return response()->json([
'success' => 'false',
'errors' => $e->getMessage(),
], 400);
}
}
控制台信息
{"success":"false","errors":["The Address Name field is required.","The Recipient field is required.","The Address field is required."]}
我可以在控制台看到错误,但在Blade中无法访问$errors变量。我该如何解决这个问题?
key
就会出现。我已经指定它只显示一个错误,我相信这符合你的需求。 - MisterP