Php Laravel 5请求-如何显示单个验证错误
我在L5中创建了一个请求来处理保存联系人的操作,如下所示:Php Laravel 5请求-如何显示单个验证错误,php,laravel,laravel-5,Php,Laravel,Laravel 5,我在L5中创建了一个请求来处理保存联系人的操作,如下所示: <?php namespace App\Http\Requests; use App\Http\Requests\Request; class AddContactRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool *
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class AddContactRequest extends Request {
/**
* 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 [
'category_ids' => 'required|array|exists:categories,id',
'gender' => 'required|in:Male,Female',
'first_name' => 'required',
'last_name' => 'required',
'company' => 'required',
'position' => 'required',
'website_url' => 'url',
'facebook_url' => 'url',
'twitter_url' => 'url',
'instagram_url' => 'url',
'misc_url1' => 'url',
'misc_url2' => 'url',
'address_1' => 'required',
'citytown' => 'required',
'postcode' => 'required',
'country_id' => 'required|integer|exists:countries,id',
'work_email' => 'email',
'home_email' => 'email',
'work_phone' => '',
'mobile_phone' => '',
'home_phone' => ''
];
}
/**
* Error messages for validation rules
* @return array
*/
public function messages()
{
return [
'category_ids.required' => 'You must select at least one category.',
'category_ids.array' => 'Categories must be an array.',
'category_ids.exists' => 'The category does not exist.',
'gender.required' => 'The gender field is required.',
'gender.in' => 'The gender is invalid.',
'first_name.required' => 'The first name field is required.',
'last_name.required' => 'The last name field is required.',
'company.required' => 'The company field is required.',
'position.required' => 'The position field is required.',
'website_url.url' => 'The website URL is not a valid URL.',
'facebook_url.url' => 'The facebook URL is not a valid URL.',
'twitter_url.url' => 'The twitter URL is not a valid URL.',
'instagram_url.url' => 'The instagram URL is not a valid URL.',
'misc_url1.url' => 'The miscellaneous URL 1 field is not a valid URL',
'misc_url2.url' => 'The miscellaneous URL 2 field is not a valid URL',
'address_1.required' => 'The address 1 field is required.',
'citytown.required' => 'The city / town field is required.',
'postcode.required' => 'The postcode field is required.',
'country_id.required' => 'The country field is required.',
'country_id.integer' => 'The country field must contain an integer.',
'country_id.exists' => 'The country is invalid.',
'work_email.email' => 'The work email field is not a valid email address.',
'home_email.email' => 'The home email field is not a valid email address.'
];
}
}
有人知道我为什么会犯这个错误吗 以单独显示错误。您应将其放置在每个元素下
<div class="error">{{ $errors->first('category_ids') }}</div>
{{$errors->first('category_id')}
注:
为此,您应该在css中创建一个名为error
的类
这将仅在任何occcurs正常的情况下显示错误,感谢Sulthan我能够找出它。而不是:
$messages->has('field_name')
我应该使用:
$errors->has('field_name')
我觉得这是一个简单的过程:
$rules=array('mobile'=>'required|numeric');
$validator=Validator::make(Input::all(),$rules);
if($validator->fails()){
$failedRules=$validator->failed();
if(isset($failedRules['signInType']['Required'])){
return response()->json(["status"=>"Error","message"=>"Please enter signInType"]);
}
if(isset($failedRules['signInType']['Numeric'])){
return response()->json(["status"=>"Error","message"=>"mobile must be numeric"]);
}
}
很抱歉我以这种方式发布代码,因为我不知道如何发布您需要的注释。因此,如果有人对你的贡献投了更高的票,你可以提出问题和回答问题并赢得声誉。这很有效,但最好使用条件(如果其他)来显示错误容器。
$messages->has('field_name')
$errors->has('field_name')
$rules=array('mobile'=>'required|numeric');
$validator=Validator::make(Input::all(),$rules);
if($validator->fails()){
$failedRules=$validator->failed();
if(isset($failedRules['signInType']['Required'])){
return response()->json(["status"=>"Error","message"=>"Please enter signInType"]);
}
if(isset($failedRules['signInType']['Numeric'])){
return response()->json(["status"=>"Error","message"=>"mobile must be numeric"]);
}
}