laravel自定义验证的问题,它是不正确的工作方式,一些错误是给定的,一些是不给定的。

问题描述 投票:0回答:1

这是我的验证代码,但没有给出适当的验证错误.它只显示这些错误。

不允许使用特殊字符,如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'
        ],
laravel laravel-validation
1个回答
0
投票

当使用 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'
     ],
);
© www.soinside.com 2019 - 2024. All rights reserved.