В форму регистрации и форму повторной отправки Email внедрён валидатор ReCaptcha

This commit is contained in:
ErickSkrauch 2016-08-03 15:57:41 +03:00
parent bef12954bd
commit c2986445f1
3 changed files with 15 additions and 4 deletions

View File

@ -61,7 +61,7 @@ class SignupController extends Controller {
'errors' => $this->normalizeModelErrors($model->getErrors()), 'errors' => $this->normalizeModelErrors($model->getErrors()),
]; ];
if ($response['errors']['email'] === E::RECENTLY_SENT_MESSAGE) { if (ArrayHelper::getValue($response['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
$activation = $model->getActivation(); $activation = $model->getActivation();
$response['data'] = [ $response['data'] = [
'canRepeatIn' => $activation->canRepeatIn(), 'canRepeatIn' => $activation->canRepeatIn(),

View File

@ -19,16 +19,23 @@ use yii\base\InvalidConfigException;
class RegistrationForm extends ApiForm { class RegistrationForm extends ApiForm {
public $captcha;
public $username; public $username;
public $email; public $email;
public $password; public $password;
public $rePassword; public $rePassword;
public $rulesAgreement; public $rulesAgreement;
public $lang; public $lang;
public function rules() { public function rules() {
return [ return [
[[], ReCaptchaValidator::class, 'message' => E::CAPTCHA_INVALID, 'when' => !YII_ENV_TEST], ['captcha', ReCaptchaValidator::class],
['rulesAgreement', 'required', 'message' => E::RULES_AGREEMENT_REQUIRED], ['rulesAgreement', 'required', 'message' => E::RULES_AGREEMENT_REQUIRED],
['username', 'validateUsername', 'skipOnEmpty' => false], ['username', 'validateUsername', 'skipOnEmpty' => false],

View File

@ -1,6 +1,7 @@
<?php <?php
namespace api\models\authentication; namespace api\models\authentication;
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
use api\models\base\ApiForm; use api\models\base\ApiForm;
use common\helpers\Error as E; use common\helpers\Error as E;
use common\components\UserFriendlyRandomKey; use common\components\UserFriendlyRandomKey;
@ -12,12 +13,15 @@ use yii\base\ErrorException;
class RepeatAccountActivationForm extends ApiForm { class RepeatAccountActivationForm extends ApiForm {
public $captcha;
public $email; public $email;
private $emailActivation; private $emailActivation;
public function rules() { public function rules() {
return [ return [
['captcha', ReCaptchaValidator::class],
['email', 'filter', 'filter' => 'trim'], ['email', 'filter', 'filter' => 'trim'],
['email', 'required', 'message' => E::EMAIL_REQUIRED], ['email', 'required', 'message' => E::EMAIL_REQUIRED],
['email', 'validateEmailForAccount'], ['email', 'validateEmailForAccount'],
@ -26,7 +30,7 @@ class RepeatAccountActivationForm extends ApiForm {
} }
public function validateEmailForAccount($attribute) { public function validateEmailForAccount($attribute) {
if (!$this->hasErrors($attribute)) { if (!$this->hasErrors()) {
$account = $this->getAccount(); $account = $this->getAccount();
if ($account === null) { if ($account === null) {
$this->addError($attribute, E::EMAIL_NOT_FOUND); $this->addError($attribute, E::EMAIL_NOT_FOUND);
@ -40,7 +44,7 @@ class RepeatAccountActivationForm extends ApiForm {
} }
public function validateExistsActivation($attribute) { public function validateExistsActivation($attribute) {
if (!$this->hasErrors($attribute)) { if (!$this->hasErrors()) {
$activation = $this->getActivation(); $activation = $this->getActivation();
if ($activation !== null && !$activation->canRepeat()) { if ($activation !== null && !$activation->canRepeat()) {
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE); $this->addError($attribute, E::RECENTLY_SENT_MESSAGE);