|
- <?php
-
-
- namespace yii\widgets;
-
- use Yii;
- use yii\base\Component;
- use yii\base\ErrorHandler;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- use yii\base\Model;
- use yii\web\JsExpression;
-
-
- class ActiveField extends Component
- {
-
-
- public $form;
-
-
- public $model;
-
-
- public $attribute;
-
-
- public $options = ['class' => 'form-group'];
-
-
- public $template = "{label}\n{input}\n{hint}\n{error}";
-
-
- public $inputOptions = ['class' => 'form-control'];
-
-
- public $errorOptions = ['class' => 'help-block'];
-
-
- public $labelOptions = ['class' => 'control-label'];
-
-
- public $hintOptions = ['class' => 'hint-block'];
-
-
- public $enableClientValidation;
-
-
- public $enableAjaxValidation;
-
-
- public $validateOnChange;
-
-
- public $validateOnBlur;
-
-
- public $validateOnType;
-
-
- public $validationDelay;
-
-
- public $selectors = [];
-
-
- public $parts = [];
-
-
-
- private $_inputId;
-
-
- private $_skipLabelFor = false;
-
-
-
-
- public function __toString()
- {
-
-
- try {
- return $this->render();
- } catch (\Exception $e) {
- ErrorHandler::convertExceptionToError($e);
- return '';
- }
- }
-
-
-
- public function render($content = null)
- {
- if ($content === null) {
- if (!isset($this->parts['{input}'])) {
- $this->textInput();
- }
- if (!isset($this->parts['{label}'])) {
- $this->label();
- }
- if (!isset($this->parts['{error}'])) {
- $this->error();
- }
- if (!isset($this->parts['{hint}'])) {
- $this->hint(null);
- }
- $content = strtr($this->template, $this->parts);
- } elseif (!is_string($content)) {
- $content = call_user_func($content, $this);
- }
-
- return $this->begin() . "\n" . $content . "\n" . $this->end();
- }
-
-
-
- public function begin()
- {
- if ($this->form->enableClientScript) {
- $clientOptions = $this->getClientOptions();
- if (!empty($clientOptions)) {
- $this->form->attributes[] = $clientOptions;
- }
- }
-
- $inputID = $this->getInputId();
- $attribute = Html::getAttributeName($this->attribute);
- $options = $this->options;
- $class = isset($options['class']) ? [$options['class']] : [];
- $class[] = "field-$inputID";
- if ($this->model->isAttributeRequired($attribute)) {
- $class[] = $this->form->requiredCssClass;
- }
- if ($this->model->hasErrors($attribute)) {
- $class[] = $this->form->errorCssClass;
- }
- $options['class'] = implode(' ', $class);
- $tag = ArrayHelper::remove($options, 'tag', 'div');
-
- return Html::beginTag($tag, $options);
- }
-
-
-
- public function end()
- {
- return Html::endTag(ArrayHelper::keyExists('tag', $this->options) ? $this->options['tag'] : 'div');
- }
-
-
-
- public function label($label = null, $options = [])
- {
- if ($label === false) {
- $this->parts['{label}'] = '';
- return $this;
- }
-
- $options = array_merge($this->labelOptions, $options);
- if ($label !== null) {
- $options['label'] = $label;
- }
-
- if ($this->_skipLabelFor) {
- $options['for'] = null;
- }
-
- $this->parts['{label}'] = Html::activeLabel($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function error($options = [])
- {
- if ($options === false) {
- $this->parts['{error}'] = '';
- return $this;
- }
- $options = array_merge($this->errorOptions, $options);
- $this->parts['{error}'] = Html::error($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function hint($content, $options = [])
- {
- if ($content === false) {
- $this->parts['{hint}'] = '';
- return $this;
- }
-
- $options = array_merge($this->hintOptions, $options);
- if ($content !== null) {
- $options['hint'] = $content;
- }
- $this->parts['{hint}'] = Html::activeHint($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function input($type, $options = [])
- {
- $options = array_merge($this->inputOptions, $options);
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeInput($type, $this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function textInput($options = [])
- {
- $options = array_merge($this->inputOptions, $options);
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeTextInput($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function hiddenInput($options = [])
- {
- $options = array_merge($this->inputOptions, $options);
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeHiddenInput($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function passwordInput($options = [])
- {
- $options = array_merge($this->inputOptions, $options);
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activePasswordInput($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function fileInput($options = [])
- {
-
- if ($this->inputOptions !== ['class' => 'form-control']) {
- $options = array_merge($this->inputOptions, $options);
- }
-
- if (!isset($this->form->options['enctype'])) {
- $this->form->options['enctype'] = 'multipart/form-data';
- }
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeFileInput($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function textarea($options = [])
- {
- $options = array_merge($this->inputOptions, $options);
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeTextarea($this->model, $this->attribute, $options);
-
- return $this;
- }
-
-
-
- public function radio($options = [], $enclosedByLabel = true)
- {
- if ($enclosedByLabel) {
- $this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options);
- $this->parts['{label}'] = '';
- } else {
- if (isset($options['label']) && !isset($this->parts['{label}'])) {
- $this->parts['{label}'] = $options['label'];
- if (!empty($options['labelOptions'])) {
- $this->labelOptions = $options['labelOptions'];
- }
- }
- unset($options['labelOptions']);
- $options['label'] = null;
- $this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options);
- }
- $this->adjustLabelFor($options);
-
- return $this;
- }
-
-
-
- public function checkbox($options = [], $enclosedByLabel = true)
- {
- if ($enclosedByLabel) {
- $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
- $this->parts['{label}'] = '';
- } else {
- if (isset($options['label']) && !isset($this->parts['{label}'])) {
- $this->parts['{label}'] = $options['label'];
- if (!empty($options['labelOptions'])) {
- $this->labelOptions = $options['labelOptions'];
- }
- }
- unset($options['labelOptions']);
- $options['label'] = null;
- $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
- }
- $this->adjustLabelFor($options);
-
- return $this;
- }
-
-
-
- public function dropDownList($items, $options = [])
- {
- $options = array_merge($this->inputOptions, $options);
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeDropDownList($this->model, $this->attribute, $items, $options);
-
- return $this;
- }
-
-
-
- public function listBox($items, $options = [])
- {
- $options = array_merge($this->inputOptions, $options);
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeListBox($this->model, $this->attribute, $items, $options);
-
- return $this;
- }
-
-
-
- public function checkboxList($items, $options = [])
- {
- $this->adjustLabelFor($options);
- $this->_skipLabelFor = true;
- $this->parts['{input}'] = Html::activeCheckboxList($this->model, $this->attribute, $items, $options);
-
- return $this;
- }
-
-
-
- public function radioList($items, $options = [])
- {
- $this->adjustLabelFor($options);
- $this->_skipLabelFor = true;
- $this->parts['{input}'] = Html::activeRadioList($this->model, $this->attribute, $items, $options);
-
- return $this;
- }
-
-
-
- public function widget($class, $config = [])
- {
-
- $config['model'] = $this->model;
- $config['attribute'] = $this->attribute;
- $config['view'] = $this->form->getView();
- if (isset($config['options']) && isset(class_parents($class)['yii\widgets\InputWidget'])) {
- $this->adjustLabelFor($config['options']);
- }
- $this->parts['{input}'] = $class::widget($config);
-
- return $this;
- }
-
-
-
- protected function adjustLabelFor($options)
- {
- if (!isset($options['id'])) {
- return;
- }
- $this->_inputId = $options['id'];
- if (!isset($this->labelOptions['for'])) {
- $this->labelOptions['for'] = $options['id'];
- }
- }
-
-
-
- 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) {
-
- $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 = $this->getInputId();
- $options['id'] = Html::getInputId($this->model, $this->attribute);
- $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) . '}');
- }
-
-
- return array_diff_assoc($options, [
- 'validateOnChange' => true,
- 'validateOnBlur' => true,
- 'validateOnType' => false,
- 'validationDelay' => 500,
- 'encodeError' => true,
- 'error' => '.help-block',
- ]);
- }
-
-
-
- protected function getInputId()
- {
- return $this->_inputId ?: Html::getInputId($this->model, $this->attribute);
- }
- }
|