示例#1
0
 /**
  * Returns the JS options for the field.
  * @return array the JS options
  */
 protected function getClientOptions()
 {
     $attribute = Html::getAttributeName($this->attribute);
     if (!in_array($attribute, $this->model->activeAttributes(), true)) {
         return [];
     }
     $enableClientValidation = $this->enableClientValidation || $this->enableClientValidation === null && $this->form->enableClientValidation;
     $enableAjaxValidation = $this->enableAjaxValidation || $this->enableAjaxValidation === null && $this->form->enableAjaxValidation;
     if ($enableClientValidation) {
         $validators = [];
         foreach ($this->model->getActiveValidators($attribute) as $validator) {
             /* @var $validator \yii\validators\Validator */
             $js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView());
             if ($validator->enableClientValidation && $js != '') {
                 if ($validator->whenClient !== null) {
                     $js = "if (({$validator->whenClient})(attribute, value)) { {$js} }";
                 }
                 $validators[] = $js;
             }
         }
     }
     if (!$enableAjaxValidation && (!$enableClientValidation || empty($validators))) {
         return [];
     }
     $options = [];
     $inputID = Html::getInputId($this->model, $this->attribute);
     $options['id'] = $inputID;
     $options['name'] = $this->attribute;
     $options['container'] = isset($this->selectors['container']) ? $this->selectors['container'] : ".field-{$inputID}";
     $options['input'] = isset($this->selectors['input']) ? $this->selectors['input'] : "#{$inputID}";
     if (isset($this->selectors['error'])) {
         $options['error'] = $this->selectors['error'];
     } elseif (isset($this->errorOptions['class'])) {
         $options['error'] = '.' . implode('.', preg_split('/\\s+/', $this->errorOptions['class'], -1, PREG_SPLIT_NO_EMPTY));
     } else {
         $options['error'] = isset($this->errorOptions['tag']) ? $this->errorOptions['tag'] : 'span';
     }
     $options['encodeError'] = !isset($this->errorOptions['encode']) || $this->errorOptions['encode'];
     if ($enableAjaxValidation) {
         $options['enableAjaxValidation'] = true;
     }
     foreach (['validateOnChange', 'validateOnBlur', 'validateOnType', 'validationDelay'] as $name) {
         $options[$name] = $this->{$name} === null ? $this->form->{$name} : $this->{$name};
     }
     if (!empty($validators)) {
         $options['validate'] = new JsExpression("function (attribute, value, messages, deferred, \$form) {" . implode('', $validators) . '}');
     }
     // only get the options that are different from the default ones (set in yii.activeForm.js)
     return array_diff_assoc($options, ['validateOnChange' => true, 'validateOnBlur' => true, 'validateOnType' => false, 'validationDelay' => 500, 'encodeError' => true, 'error' => '.help-block']);
 }