1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\jquery\validators\client; |
9
|
|
|
|
10
|
|
|
use yii\jquery\ValidationAsset; |
11
|
|
|
use yii\validators\client\ClientValidator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* CaptchaClientValidator composes client-side validation code from [[CaptchaValidator]]. |
15
|
|
|
* |
16
|
|
|
* @see CaptchaValidator |
17
|
|
|
* @see ValidationAsset |
18
|
|
|
* |
19
|
|
|
* @author Paul Klimov <[email protected]> |
20
|
|
|
* @since 2.1.0 |
21
|
|
|
*/ |
22
|
|
|
class CaptchaClientValidator extends ClientValidator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function build($validator, $model, $attribute, $view) |
28
|
|
|
{ |
29
|
|
|
ValidationAsset::register($view); |
30
|
|
|
$options = $this->getClientOptions($validator, $model, $attribute); |
|
|
|
|
31
|
|
|
return 'yii.validation.captcha(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the client-side validation options. |
36
|
|
|
* @param \yii\captcha\CaptchaValidator $validator the server-side validator. |
37
|
|
|
* @param \yii\base\Model $model the model being validated |
38
|
|
|
* @param string $attribute the attribute name being validated |
39
|
|
|
* @return array the client-side validation options |
40
|
|
|
*/ |
41
|
|
|
public function getClientOptions($validator, $model, $attribute) |
42
|
|
|
{ |
43
|
|
|
$captcha = $validator->createCaptchaAction(); |
44
|
|
|
$code = $captcha->getVerifyCode(false); |
45
|
|
|
$hash = $captcha->generateValidationHash($validator->caseSensitive ? $code : strtolower($code)); |
46
|
|
|
$options = [ |
47
|
|
|
'hash' => $hash, |
48
|
|
|
'hashKey' => 'yiiCaptcha/' . $captcha->getUniqueId(), |
49
|
|
|
'caseSensitive' => $validator->caseSensitive, |
50
|
|
|
'message' => $validator->formatMessage($validator->message, [ |
51
|
|
|
'attribute' => $model->getAttributeLabel($attribute), |
52
|
|
|
]), |
53
|
|
|
]; |
54
|
|
|
if ($validator->skipOnEmpty) { |
55
|
|
|
$options['skipOnEmpty'] = 1; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $options; |
59
|
|
|
} |
60
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.