Due to the default throttle middleware that will return its own response denying any access to the route you provide it after a certain number of requests, this is a throttle validation rule allowing the throttling of form requests so you're able to restrict only the submission of forms for the specific field of your choice.
For example, maybe a contact form with an email address can only be filled out once every 10 minutes per user.
Here is how you would perform this:
class ContactRequest 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()
{
return [
'email' => [
'required',
'email',
new Throttle('contact-form', $maxAttempts = 1, $minutes = 10),
],
'message' => [
'required',
'min:10',
'max:250',
],
];
}
Once the user has tried more than once in 10 minutes, a validation
error of "Too many attempts. Please try again later." will be
returned to the form under the email
key.
Note for users of recent Laravel version, the cache ttl unit is no longer minutes but seconds.
Thus the
$decayInMinutes
field is actually seconds, thus value you supply need to be multiplied by 60 to be in minutes