/** * 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 widget. */ public function run() { if ($this->hasModel() && $this->activeField) { $this->options = $this->activeField->calculateClientInputOption($this->options); $input = ActiveHtml::activeTextInput($this->model, $this->attribute, $this->options); } else { $input = Html::textInput($this->name, $this->value, $this->options); } if ($this->output === self::BASE64) { $src = $this->captcha->getDataUri(); } else { $src = Url::modify([$this->captchaAction, 'v' => uniqid()], Url::ABS); } $image = Html::img($src, $this->imageOptions); echo strtr($this->template, ['{input}' => $input, '{image}' => $image]); }
/** * Returns all uploaded files for the given model attribute. * * @param \rock\components\Model $model the data model * @param string $attribute the attribute name. The attribute name may contain array indexes * for tabular file uploading, e.g. '[1]file'. * @return UploadedFile[] array of UploadedFile objects. * Empty array is returned if no available file was found for the given attribute. * @throws FileException * @throws \rock\template\HtmlException */ public static function getInstances($model, $attribute) { if (!class_exists('\\rock\\widgets\\ActiveHtml')) { throw new FileException(FileException::NOT_INSTALL_TEMPLATE); } $name = ActiveHtml::getInputName($model, $attribute); return static::getInstancesByName($name); }
private function _calculateParts() { if (isset($this->parts['{input}']) && $this->parts['{input}'] === '') { return ''; } if (!isset($this->parts['{input}'])) { $this->inputOptions = $this->calculateClientInputOption($this->inputOptions); $this->parts['{input}'] = ActiveHtml::activeTextInput($this->model, $this->attribute, $this->inputOptions); } if (!isset($this->parts['{label}'])) { $this->parts['{label}'] = ActiveHtml::activeLabel($this->model, $this->attribute, $this->labelOptions); } if (!isset($this->parts['{error}'])) { $this->parts['{error}'] = $this->renderErrors(); } if (!isset($this->parts['{hint}'])) { $this->parts['{hint}'] = ActiveHtml::activeHint($this->model, $this->attribute, $this->hintOptions); } return strtr($this->template, $this->parts); }
/** * 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; }