/** * Initializes the widget. * If you override this method, make sure you call the parent implementation first. */ public function init() { if (!$this->hasModel() && $this->name === null) { throw new WidgetException("Either 'name', or 'model' and 'attribute' properties must be specified."); } if (!isset($this->options['id'])) { $this->options['id'] = $this->hasModel() ? ActiveHtml::getInputId($this->model, $this->attribute) : $this->getId(); } parent::init(); }
/** * Renders the opening tag of the field container. * * @return string the rendering result. */ public function begin() { $inputID = ActiveHtml::getInputId($this->model, $this->attribute); $attribute = ActiveHtml::getAttributeName($this->attribute); $options = $this->options; $class = isset($options['class']) ? [$options['class']] : []; $class[] = "field-{$inputID}"; if (isset($this->form) && $this->model->isAttributeRequired($attribute)) { $class[] = $this->form->requiredCssClass; } if ($this->model->hasErrors($attribute) && isset($this->form)) { $class[] = $this->form->errorCssClass; } $options['class'] = implode(' ', $class); $tag = ActiveHtml::remove($options, 'tag', 'div'); return ActiveHtml::beginTag($tag, $options); }
/** * 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: * * ```php * // ... load $models ... * if (Rock::$app->request->isAjax) { * Rock::$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 \rock\components\Model $model */ foreach ($models as $i => $model) { $model->validate($attributes); foreach ($model->getErrors() as $attribute => $errors) { $result[ActiveHtml::getInputId($model, "[{$i}]" . $attribute)] = $errors; } } return $result; }