Esempio n. 1
0
 /**
  * Initializes the widget.
  * If you override this method, make sure you call the parent implementation first.
  */
 public function init()
 {
     if ($this->name === null && !$this->hasModel()) {
         throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
     }
     if (!isset($this->options['id'])) {
         $this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
     }
     parent::init();
 }
Esempio n. 2
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 \Leaps\Validator\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 leaps.activeForm.js)
     return array_diff_assoc($options, ['validateOnChange' => true, 'validateOnBlur' => true, 'validateOnType' => false, 'validationDelay' => 500, 'encodeError' => true, 'error' => '.help-block']);
 }
 /**
  * @inheritdoc
  */
 public function clientValidateAttribute($model, $attribute, $view)
 {
     $options = ['operator' => $this->operator, 'type' => $this->type];
     if ($this->compareValue !== null) {
         $options['compareValue'] = $this->compareValue;
         $compareValue = $this->compareValue;
     } else {
         $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
         $compareValue = $model->getAttributeLabel($compareAttribute);
         $options['compareAttribute'] = Html::getInputId($model, $compareAttribute);
     }
     if ($this->skipOnEmpty) {
         $options['skipOnEmpty'] = 1;
     }
     $options['message'] = Leaps::$app->getI18n()->format($this->message, ['attribute' => $model->getAttributeLabel($attribute), 'compareAttribute' => $compareValue, 'compareValue' => $compareValue], Leaps::$app->language);
     ValidationAsset::register($view);
     return 'leaps.validation.compare(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
 }
Esempio n. 4
0
 /**
  * Validates an array of model instances and returns an error message array indexed by the attribute IDs.
  * This is a helper method that simplifies the way of writing AJAX validation code for tabular input.
  *
  * For example, you may use the following code in a controller action to respond
  * to an AJAX validation request:
  *
  * ~~~
  * // ... load $models ...
  * if (Leaps::$app->request->isAjax) {
  *     Leaps::$app->response->format = Response::FORMAT_JSON;
  *     return ActiveForm::validateMultiple($models);
  * }
  * // ... respond to non-AJAX request ...
  * ~~~
  *
  * @param array $models an array of models to be validated.
  * @param mixed $attributes list of attributes that should be validated.
  * If this parameter is empty, it means any attribute listed in the applicable
  * validation rules should be validated.
  * @return array the error message array indexed by the attribute IDs.
  */
 public static function validateMultiple($models, $attributes = null)
 {
     $result = [];
     /* @var $model Model */
     foreach ($models as $i => $model) {
         $model->validate($attributes);
         foreach ($model->getErrors() as $attribute => $errors) {
             $result[Html::getInputId($model, "[{$i}]" . $attribute)] = $errors;
         }
     }
     return $result;
 }