laravel подтвердить дату, состоящую из 3 входов - PullRequest
0 голосов
/ 17 марта 2020

На веб-сайте, который я создаю, я спрашиваю у пользователя дату его рождения, в 3 разных входах,

<input name="day" type="text" />
<input name="month" type="text" />
<input name="year" type="text" />

Я проверяю каждый отдельный ввод, но мне нужно подтвердить, что если у него есть входные данные во все 3 поля (день и месяц являются необязательными), это дата в прошлом, а не в будущем, это мой класс запросов в его текущем состоянии,

public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'firstnames' => 'required',
        'lastname' => 'required',
        'dob_day' => 'digits_between:1,2|nullable|between:1,31',
        'dob_month' => 'digits_between:1,2|nullable|between:1,12',
        'dob_year' => 'digits:4|required',
        //need to validate the date of birth if all 3 have values.
    ];
}

/**
 * Get the validation messages that apply to the rules
 *
 * @return array
 */
public function messages()
{
    return [
        'firstnames.required' => 'Please enter any first names',
        'lastname.required' => 'Please enter a last name',
        //'birth_place.required' => 'Please enter a place of birth',
        'dob_month.digits_between' => 'The date of birth\'s month must be no more than 2 characters in length',
        'dob_day.digits_between' => 'The date of birth\'s day must be no more than 2 characters in length',
        'dob_month.max' => 'The date of birth\'s month must be no more than 2 characters in length',
        'dob_year.digits' => 'The date of birth\'s year must be 4 characters in length',
        'dob_year.required' => 'Please enter a year of birth, even if it is an estimate',
        //'dob_accurate.required' => 'Please specify whether the date of birth is accurate'
    ];
}

Могу ли я проверить три различные входные данные, как 1, и выбросить и ошибка, если дата недействительна как дата в прошлом?

1 Ответ

0 голосов
/ 17 марта 2020

Вы можете использовать javascript, чтобы установить дату в другое скрытое поле, которое вы проверяете как в прошлом, если остальные заполнены. Или, если вы не хотите использовать javascript, вы можете выполнить дополнительную проверку в операторе if для входных данных текущей даты и изменить их правила, чтобы они соответствовали или выдавали исключение

public function rules()
{
    $rules = [
        'firstnames' => 'required',
        'lastname' => 'required',
        'dob_day' => ['digits_between:1,2', 'nullable', 'between:1,31'],
        'dob_month' => ['digits_between:1,2', 'nullable', 'between:1,12'],
        'dob_year' => ['digits:4', 'required']
        //need to validate the date of birth if all 3 have values.
    ];
    if (request()->has('dob_day')&&request()->has('dob_month')&&request()->has('dob_year')) {
        $date = now();
        $rules['full_date'] = 'before:'.now()->toDateString();

        //Or if you don't want to use javascript you can do some extra checking here on the current date inputs and change their rules to fit or throw an exception
        $rules['dob_year'][] = 'max:'.now()->year;
        ...
    }
    return $rules
}
...