Я передаю два поля в мой API max_duration
и min_duration
, и я использую FormRequest Laravel для выполнения валидации. Вот мое РекомендацииRequest.php класс:
{
/**
* 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()
{
return [
"max_duration" => ["integer", new ValidateMaxDuration],
"min_duration" => ["integer", new ValidateMinDuration]
];
}
}
Вот мое ValidateMaxDuration.php
Правило, которое я хочу проверить, что значение max_duration
выше, чем значение min_duration
. Как я могу это сделать?
class ValidateMaxDuration implements Rule
{
private $minDuration;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
// $this->minDuration = $minDuration;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
// logic done here to validate max value
// return $value[0] < $value[1] ? false : true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return "Max duration must be a higher value than min duration.";
}
}