Laravel Validator有时是有条件的

时间:2017-09-16 12:29:00

标签: php laravel

我试图测试以确定从表单输入的日期是否早于或等于数据库中的日期。只有在另一个条件成立时才需要这样做,所以我在验证器上使用了有时候的方法。

我是否正确执行此逻辑?

<?php

namespace App\Http\Requests;

use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UserEditFormRequest 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()
     {
        //dd($this->all());
        return [
            'hired_at' => 'required|date'
        ];
     }

    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return void
     */
    public function withValidator($validator)
    {
        if ($this->user->hasPosts()) {
            $validator->sometimes('hired_at', 'before_or_equal:'.$this->user->firstPostDate(), function($input) {
                //return true;
            });
        }
    }

}

0 个答案:

没有答案