I finally succeded to get the captcha widget working in the login
form of in a Yii2 Users
module, but in the register
form the captcha
field only validates correctly, when calling $user->validate()
, but not when calling $user->save()
.
if ($user->load(Yii::$app->request->post()) && $user->validate()) /* $user->validate() succeeds. */
{
$user->hash = Yii::$app->security->generatePasswordHash($user->password);
$user->auth_key = Yii::$app->security->generateRandomString();
if ($user->save()) /* $user->save() does not succeed. */
{
The problem seems to be within the validate()
method of the Model
class, which returns false
, after looping the CaptchaValidator
:
public function validate($attributeNames = null, $clearErrors = true)
{
...
foreach ($this->getActiveValidators() as $validator) {
$validator->validateAttributes($this, $attributeNames);
}
$this->afterValidate();
return !$this->hasErrors(); /* Returns false. */
Thanks