1 | <?php |
||
26 | abstract class ActiveFormClientScript extends Behavior |
||
27 | { |
||
28 | /** |
||
29 | * @var array client validator class map in format: `[server-side-validator-class => client-side-validator]`. |
||
30 | * Client side validator should be specified as an instance of [[\yii\validators\client\ClientValidator]] or |
||
31 | * its DI compatible configuration. |
||
32 | * |
||
33 | * Class map respects validators inheritance, e.g. if you specify map for `ParentValidator` it will be used for |
||
34 | * `ChildValidator` in case it extends `ParentValidator`. In case maps for both `ParentValidator` and `ChildValidator` |
||
35 | * are specified the first value will take precedence. |
||
36 | * |
||
37 | * Result of [[defaultClientValidatorMap()]] method will be merged into this field at behavior initialization. |
||
38 | * In order to disable mapping for pre-defined validator use `false` value. |
||
39 | * |
||
40 | * For example: |
||
41 | * |
||
42 | * ```php |
||
43 | * [ |
||
44 | * \yii\validators\BooleanValidator::class => \yii\jquery\validators\client\BooleanValidator::class, |
||
45 | * \yii\validators\ImageValidator::class => \yii\jquery\validators\client\ImageValidator::class, |
||
46 | * \yii\validators\FileValidator::class => false, // disable client validation for `FileValidator` |
||
47 | * ] |
||
48 | * ``` |
||
49 | * |
||
50 | * You should use this field only in case particular client script does not provide any default mapping or |
||
51 | * in case you wish to override this mapping. |
||
52 | */ |
||
53 | public $clientValidatorMap = []; |
||
54 | |||
55 | /** |
||
56 | * @var array the client validation options for individual attributes. Each element of the array |
||
57 | * represents the validation options for a particular attribute. |
||
58 | */ |
||
59 | protected $attributes = []; |
||
60 | |||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function init() |
||
66 | { |
||
67 | parent::init(); |
||
68 | |||
69 | $this->clientValidatorMap = array_merge( |
||
70 | $this->defaultClientValidatorMap(), |
||
71 | $this->clientValidatorMap |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function events() |
||
79 | { |
||
80 | return [ |
||
81 | Widget::EVENT_AFTER_RUN => 'afterRun', |
||
82 | ActiveForm::EVENT_BEFORE_FIELD_RENDER => 'beforeFieldRender', |
||
83 | ]; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Handles [[Widget::EVENT_AFTER_RUN]] event, registering related client script. |
||
88 | * @param \yii\base\Event $event event instance. |
||
89 | */ |
||
90 | public function afterRun($event) |
||
|
|||
91 | { |
||
92 | $this->registerClientScript(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Handles [[ActiveForm::EVENT_BEFORE_FIELD_RENDER]] event. |
||
97 | * @param ActiveFieldEvent $event event instance. |
||
98 | */ |
||
99 | public function beforeFieldRender($event) |
||
100 | { |
||
101 | $clientOptions = $this->getFieldClientOptions($event->field); |
||
102 | if (!empty($clientOptions)) { |
||
103 | $this->attributes[] = $clientOptions; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Registers underlying client script including JavaScript code, which supports form validation. |
||
109 | */ |
||
110 | abstract protected function registerClientScript(); |
||
111 | |||
112 | /** |
||
113 | * Returns the JS options for the field. |
||
114 | * @param ActiveField $field active field instance. |
||
115 | * @return array the JS options. |
||
116 | */ |
||
117 | protected function getFieldClientOptions($field) |
||
187 | |||
188 | /** |
||
189 | * Returns the options for the form JS widget. |
||
190 | * @return array the options. |
||
191 | */ |
||
192 | protected function getClientOptions() |
||
212 | |||
213 | /** |
||
214 | * Builds the JavaScript needed for performing client-side validation for given validator. |
||
215 | * @param \yii\validators\Validator $validator validator to be built. |
||
216 | * @param \yii\base\Model $model the data model being validated. |
||
217 | * @param string $attribute the name of the attribute to be validated. |
||
218 | * @param \yii\web\View $view the view object that is going to be used to render views or view files |
||
219 | * containing a model form with validator applied. |
||
220 | * @return string|null client-side validation JavaScript code, `null` - if given validator is not supported. |
||
221 | */ |
||
222 | protected function buildClientValidator($validator, $model, $attribute, $view) |
||
233 | |||
234 | /** |
||
235 | * Returns default client validator map, which will be merged with [[clientValidatorMap]] at [[init()]]. |
||
236 | * Child class may override this method providing validator map specific for particular client script. |
||
237 | * @return array client validator class map in format: `[server-side-validator-class => client-side-validator]`. |
||
238 | */ |
||
239 | protected function defaultClientValidatorMap() |
||
243 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.