v3
Field
The token is fetched on form submit (not on page load),
preventing unexpected challenge popups. The form is auto-resolved
via closest("form") - no formId needed
when the hidden input is inside the form.
use Yiisoft\FormModel\FormModel;
use YiiRocks\Recaptcha\RecaptchaV3Field;
use YiiRocks\Recaptcha\RecaptchaV3Badge;
echo RecaptchaV3Field::field($form, 'captcha')
->withAction('login')
->withFormId('login-form')
->withBadge(RecaptchaV3Badge::Hidden)
->render();
Options
Inherited from InputField
Container (inherited from BaseField)
Hidden badge: When Badge::Hidden is selected, the legal notice text
("This site is protected by reCAPTCHA…") is displayed automatically and
translated when a translator is available (either via withTranslator() or
through RecaptchaRegistry).
Validation
Use the attribute on your form model property:
use YiiRocks\Recaptcha\RecaptchaV3Rule;
final class LoginForm
{
#[RecaptchaV3Rule(
threshold: 0.5,
action: 'login',
sendRemoteIp: true,
)]
public string $gRecaptchaResponse = '';
}
Important: If you set ->withAction('...') on the v3 field,
you must also set action: '...' on the rule with the same value.
If neither is set, no action is sent and the check is skipped entirely.