A
A
Alex Mirgorodskiy2017-08-23 16:27:02
JavaScript
Alex Mirgorodskiy, 2017-08-23 16:27:02

How to properly connect reCapcha to yii2?

Hello people, tell me where I made a mistake, or I'm a fool or the skis don't go. I connect the reCaptcha plugin https://github.com/himiklab/yii2-recaptcha-widget, everything works, but the validation does not miss something.
In web.php file included:
// Captcha
'reCaptcha' => [
'name' => 'reCaptcha',
'class' => 'himiklab\yii2\recaptcha\ReCaptcha',
'siteKey' => '6LcyeS0UAAAAAGtTPNZ6AUyIhXXXXXXXXXX,
'secret ' => '6LcyeS0UAAAAAG0ugrwlS9p9_-9XXXXXXXXXX',
],
in the model in the rules, specified the captcha as a required field and the class of the extension itself:
public $image;
public $reCaptcha;
// Rules
public function rules()
{
return [
//Required
fields [['review','name','city','reCaptcha'], 'required', 'message' => 'Field cannot be empty ↓'],
// Check message length
[['review'], 'string', 'length' => [100, 400], 'tooLong' => 'Message must not exceed 400 characters','tooShort' => 'Message must contain at least 100 characters'],
[['name','city'], 'string'],
[['reCaptcha'], \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => '6LcyeS0UAAAAAG0ugrwlS9pXXXXXXXX', 'uncheckedMessage' => 'Please confirm that you are not a bot.']
];
}
}
and in the form:
<?php $form = ActiveForm::begin([
'id' => 'rew-form',
'layout' => 'horizontal',
'options' => ['class' => 'modal_rewiews_input','enctype' => 'multipart/form-data'],
'fieldConfig' => [
'template' => "{error}\n{input}",
<?= $form->field($model, 'image')->fileInput(['class' => 'upload modal_rewiews_input','id' => 'files'])->label(false);?>
<?= $form->field($model, 'name')->textInput(['placeholder' => 'Firstname Lastname'])->label(false) ?>
<?= $form->field($ model, 'city')->textInput(['placeholder' => 'City'])->label(false) ?>
<?= $form->field($model, 'review')->textarea([ 'rows' => 4, 'id' => 'good_reviews', 'placeholder' => 'Good reviews'])->label(false) ?>
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget( ['name'=> 'reCaptcha']) ?>
<?= Html::submitButton('Submit Feedback', ['class' => 'btn btn-primary main_btn form_btn', 'name' => 'button']) ?>
<?php $form = ActiveForm::end() ?>
if I remove reCapcha from the required fields, it skips it, but naturally then the meaning of the connection is lost in general ...
Can you tell me the correct connection order?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-08-23
@AlexWeb6667

no captcha required

public $reCaptcha;
public function rules()
{
return [
public $reCaptcha;

public function rules()
{
  return [
      // ...
      [['reCaptcha'], \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => 'your secret key', 'uncheckedMessage' => 'Please confirm that you are not a bot.']
  ];
}
];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question