我试图测试以确定从表单输入的日期是否早于或等于数据库中的日期。只有在另一个条件成立时才需要这样做,所以我在验证器上使用了有时候的方法。
我是否正确执行此逻辑?
<?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;
});
}
}
}