Php Laravel 5.5是否在验证请求文件中使用自定义验证规则?
是否可以在验证请求文件中使用我的自定义验证规则 我想使用名为EmployeeMail的自定义规则 这是请求文件的代码Php Laravel 5.5是否在验证请求文件中使用自定义验证规则?,php,laravel,validation,Php,Laravel,Validation,是否可以在验证请求文件中使用我的自定义验证规则 我想使用名为EmployeeMail的自定义规则 这是请求文件的代码 class CoachRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() {
class CoachRequest extends FormRequest
{
/**
* 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()
{
$rules = [];
if ($this->isMethod('post') ) {
$rules = [
'name' => 'required|string',
'email' => 'required|email|employeemail', <<<--- this
'till' => 'required|date_format:H:i|after:from',
];
}
//TODO fix this
//TODO add custom messages for every field
return $rules;
}
}
我只能这样使用这个自定义规则吗
$items = $request->validate([
'name' => [new FiveCharacters],
]);
鲁特维吉·科塔里在评论中回答了这个问题
似乎您正在使用正则表达式验证字符串,同样的逻辑可以通过regex内置验证方法实现。过来看。laravel.com/docs/5.5/validation#规则regex无需创建自己的验证规则鲁特维克塔里
如果要使用验证,请将其传递到数组中。像这样。”email'=>['required','email',new employeemail]似乎您正在使用正则表达式验证字符串,同样的逻辑可以通过regex内置验证方法实现。过来看。无需创建自己的验证规则。如果要使用验证规则,请将其传递到数组中。像这样<代码>'email'=>['required','email',new employeemail],@RutvijKothari啊谢谢,我会用正则表达式规则代替,我忘了它。还感谢您提到我将如何在请求文件中使用自定义规则
$items = $request->validate([
'name' => [new FiveCharacters],
]);