Валидация в Laravel 5.6 - PullRequest
       2

Валидация в Laravel 5.6

0 голосов
/ 11 декабря 2018

Этот вопрос уже задавался, но это не решает мою проблему.

Я попытался использовать проверку в моем laravel project.Но это не работает, это мой код

$rules = array(
    'coupon_title'          => 'max:10',
    'coupon_type_id'        => 'numaric',
    'expiry_start_date'     => 'required|before_or_equal:expiry_end_date',
    'expiry_end_date'       => 'required',
);

$messages = [
    'before_or_equal'    => 'The :attribute must be smaller than the End Date',
    'before'    => 'The :attribute must be Greater than the Interview Start Date',
    'after'    => 'The :attribute must be Greater than the Interview End Date',
    'after_or_equal'    => 'The :attribute must be Greater than the Start Time',
    //'max' => 'asdasd'
];

$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) 
{
    $messages = $validator->messages();
    return response()->json(['message'=>$messages],401);
}

В моем коде проверено только поле expiry_start_date.Но поля coupon_title и coupon_type_id не проверены.

1 Ответ

0 голосов
/ 11 декабря 2018

Вам необходимо исправить заклинание numaric в numeric и добавить ключевое слово nullable в правило проверки

'coupon_title'          => 'string|max:10|nullable',
'coupon_type_id'        => 'numeric',

Также вы можете добавить пользовательское сообщение для numeric и max

$messages = [
    'before_or_equal'    => 'The :attribute must be smaller than the End Date',
    'before'    => 'The :attribute must be Greater than the Interview Start Date',
    'after'    => 'The :attribute must be Greater than the Interview End Date',
    'after_or_equal'    => 'The :attribute must be Greater than the Start Time',
    'max' => 'The :attribute has crossed the limit',
    'numeric' => 'The :attribute must have numeric value'
];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...