这是我的验证代码,但没有给出适当的验证错误.它只显示这些错误。
不允许使用特殊字符,如t、:、givenName is requiredMiddleName is requiredwhile maximum field is required
$this->validate($request,[
'DisplayName'=>'required|max:500',
'DisplayName'=>'regex:/(^[A-Za-z0-9]+$)+/',
'GivenName' => 'required|max:100',
'MiddleName' =>'required|max:100',
'Title' => 'max:16',
'Suffix' =>'max:16',
'FamilyName' =>'max:100'
],
[
'DisplayName.required' => 'DisplayName is required!',
'DisplayName.regex' => 'Special character is not allowed like \t, :, \n ',
'GivenName.max' =>'GivenName is max 100 words',
'GivenName.required' =>'GivenName is required',
'MiddleName.max' =>'MiddleName is max 100 words',
'MiddleName.required' =>'MiddleName is required',
'Title.max' =>'Title is max 16 words',
'Suffix.max' =>'Suffix is max 16 words',
'FamilyName.max' =>'FamilyName is 100 words'
],
当使用 regex
模式,可能需要在一个数组中指定规则,而不是使用 管道分界线特别是当正则表达式中包含一个管道字符时,如: 。
$this->validate(request(), [
'userName' =>
array(
'required',
'regex:/(^([a-zA-Z]+)(\d+)?$)/u'
)
]);
所以你的代码应该是 :
$this->validate($request,[
'DisplayName' =>
array(
'required',
'max:500',
'regex:/(^[A-Za-z0-9]+$)+/'
),
'GivenName' => 'required|max:100',
'MiddleName' =>'required|max:100',
'Title' => 'max:16',
'Suffix' =>'max:16',
'FamilyName' =>'max:100'
],
[
'DisplayName.required' => 'DisplayName is required!',
'DisplayName.regex' => 'Special character is not allowed like \t, :, \n ',
'GivenName.max' =>'GivenName is max 100 words',
'GivenName.required' =>'GivenName is required',
'MiddleName.max' =>'MiddleName is max 100 words',
'MiddleName.required' =>'MiddleName is required',
'Title.max' =>'Title is max 16 words',
'Suffix.max' =>'Suffix is max 16 words',
'FamilyName.max' =>'FamilyName is 100 words'
],
);