Skip to main content

Validation

Laravel Captcha comes with a robust validation mechanism. This includes a getResponseName method to retrieve the response from the Captcha server and a CaptchaRule for form validation. You can also use these in combination with Laravel's in-built validation rules.

Captcha Response

The getResponseName is used to get the response from the captcha server. This method is useful for validating the captcha data in the server.

Here is how you can use it:

use Rahul900day\Captcha\Facades\Captcha;

$response = Captcha::getResponseName();

Captcha Rule

The Captcha Rule is a crucial part of the validation process. It validates the captcha response data received from the client against the data on the captcha server. This ensures that the captcha was solved correctly by the user.

Here is how to use the CaptchaRule in validation:

use Rahul900day\Captcha\Rules\Captcha as CaptchaRule;
...

$validator = Validator::make($request->all(), [
Captcha::getResponseName() => [
'required',
new CaptchaRule(),
]
]);

Laravel Validation

Laravel Captcha integrates seamlessly with Laravel's built-in validation system. You can use the 'captcha' custom validation rule. It is aliased to CaptchaRule and can be used in place of new CaptchaRule() in validation rules.

Here is an instance of how it can be used:

$validator = Validator::make($request->all(), [
Captha::getResponseName() => 'required|captcha'
]);

In the code snippet above, Captcha::getResponseName() will return the name of the response variable and required|captcha ensures that the captcha value is both present in the request and valid as per the captcha service's validation.

Custom Validation Message

It's possible to customize the validation error message that appears when the captcha validation fails. This is particularly useful if you want the error message to match the stylistic tone of your application or to provide more user-friendly error information.

To set a custom validation error message, you can modify the $message property of the Captcha Rule within your AppServiceProvider. Here's how you can do it:

app/Providers/AppServiceProvider
use Rahul900day\Captcha\Rules\Captcha;

Captcha::$message = 'Your Custom Captcha Error Message';

This will replace the default validation message for failed captcha with 'Your Custom Captcha Error Message'.

Localized Error Messages

If your application supports multiple languages, you may want to define the error message in different languages. Laravel allows you to define validation messages in your language files, which are located in the resources/lang directory.

For instance, if you want to provide a Spanish error message when captcha validation fails, you can define the error message in the resources/lang/es/validation.php file:

return [
// Other validation messages...

'Your Custom Captcha Error Message' => 'Su mensaje de error de Captcha personalizado',
];

In this example, 'Su mensaje de error de Captcha personalizado' is your custom error message translated into Spanish. Laravel will automatically use the correct language file based on your application's locale setting.