In this episode, we focus on how to actually validate the Turnstile response within our Laravel Livewire application. We start out by trying the usual Livewire way—wanting to add a custom rule using attributes. However, because the Turnstile rule comes from a package and is a custom validation rule, it doesn't fit neatly into Livewire's standard validation attributes.
So, instead, we switch to the classic approach: defining a rules()
method right on the component. This method returns an array configuring validation for our form, like making sure the email is both required and correctly formatted. We also add the Turnstile response to our rules, marking it as required and using the custom rule from the package to actually check it with Cloudflare.
After getting the basics set up, we actually submit the form to see it in action. We try the happy path, passing validation after successfully completing the Turnstile challenge. Then, we purposely send an invalid response to see what happens, confirming that the validation fails as we expect (and no bad data gets through).
Finally, we make things a bit friendlier by outputting the actual error message that comes back when the validation fails, so users get feedback like “the response parameter is invalid or has expired.” We wrap things up by cleaning up the test code, confirming we’re all set for the next step where we’ll look into customizing these error messages.