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.
Hi @y-eight, you can then use the
bail
validation rule, which will prevent subsequent rules from executing when the first rule fails.This prevents the throttle from being hit until all rules pass.
https://laravel.com/docs/8.x/validation#rule-bail