/** * @inheritDoc */ public function render($content = null) { if ($content === null) { if (!isset($this->parts['{beginWrapper'])) { $options = $this->wrapperOptions; $tag = ArrayHelper::remove($options, 'tag', 'div'); $this->parts['{beginWrapper}'] = Html::beginTag($tag, $options); $this->parts['{endWrapper}'] = Html::endTag($tag); } if ($this->label === false) { $this->parts['{label}'] = ''; $this->parts['{beginLabel}'] = ''; $this->parts['{labelTitle}'] = ''; $this->parts['{endLabel}'] = ''; } elseif (!isset($this->parts['{beginLabel'])) { $this->parts['{beginLabel}'] = Html::beginTag('label', $this->labelOptions); $this->parts['{endLabel}'] = Html::endTag('label'); $attribute = Html::getAttributeName($this->attribute); $this->parts['{labelTitle}'] = Html::encode($this->label ? $this->label : $this->model->getAttributeLabel($attribute)); } if ($this->inputTemplate) { $input = isset($this->parts['{input}']) ? $this->parts['{input}'] : Html::activeTextInput($this->model, $this->attribute, $this->inputOptions); $this->parts['{input}'] = strtr($this->inputTemplate, ['{input}' => $input]); } } return parent::render($content); }
public function header($title = null, $options = []) { if ($title === false) { // 为false则不显示 $this->parts['{header}'] = ''; return $this; } $options = array_merge($this->headerOptions, $options); $attribute = Html::getAttributeName($this->attribute); if ($title !== null) { $options['title'] = $title; } $title = isset($options['title']) ? $options['title'] : Html::encode($this->model->getAttributeLabel($attribute)); if ($this->collapsed == true) { $faclass = "fa-plus"; } else { $faclass = "fa-minus"; } $content = '<h3 class="box-title">' . $title . '</h3> <div class="box-tools pull-right"> <button class="btn btn-box-tool" data-widget="collapse"><i class="fa ' . $faclass . '"></i></button> </div>'; $this->parts['{header}'] = Html::tag("div", $content, $options); return $this; }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->pluginLoading) { $this->_loadIndicator = self::LOAD_PROGRESS; } if ($this->hasModel()) { $this->name = ArrayHelper::remove($this->options, 'name', Html::getInputName($this->model, $this->attribute)); $this->value = $this->model[Html::getAttributeName($this->attribute)]; } $view = $this->getView(); if (!isset($this->language)) { $this->language = Yii::$app->language; } WidgetAsset::register($view); }
<?php use yii\helpers\Html; use kartik\select2\Select2; \app\widgets\Select3Assets::register($this); $pureAttribute = Html::getAttributeName($attribute); $secondPureAttribute = Html::getAttributeName($secondAttribute); $error = ($model->hasErrors($attribute) or $model->hasErrors($secondAttribute)) ? 'has-error' : ''; echo '<div id="" class="form-group ' . $error . '">'; echo Html::activeLabel($model, $attribute); // if ($model->$pureAttribute) // echo $this->registerJs(' // $(function(){ // var select2Inputid = "'.Html::getInputId($model,$attribute).'"; // var is_create = false; // while(!is_create){ // if ($("#"+select2Inputid) != undefined){ // is_create = true; // $("#"+select2Inputid).select2("val","'.$model->$pureAttribute.'"); // } // } // }); // '); echo Select2::widget(['model' => $model, 'attribute' => $attribute, 'data' => $data, 'value' => $model->{$pureAttribute}, 'options' => ['placeholder' => $placeholder], 'pluginEvents' => ['change' => 'function (){ var IdInput = \'' . Html::getInputId($model, $attribute) . '\'; var NazvanieInput = \'' . Html::getInputId($model, $secondAttribute) . '\'; if ($(\'#\'+IdInput).val()){ $(\'#\'+NazvanieInput).val(""); } }']]); if (isset($model->{$secondAttribute})) {
/** * Renders a static input (display only). * * @param array $options the tag options in terms of name-value pairs. * @return ActiveField object */ public function staticInput($options = []) { Html::addCssClass($options, 'form-control-static'); $content = isset($this->model[Html::getAttributeName($this->attribute)]) ? $this->model[Html::getAttributeName($this->attribute)] : '-'; $this->parts['{input}'] = Html::tag('p', $content, $options); return $this; }
/** * Renders the special HTML5 input * Mainly useful for the color and range inputs */ protected function renderInput() { Html::addCssClass($this->options, 'form-control'); $size = isset($this->size) ? ' input-group-' . $this->size : ''; Html::addCssClass($this->containerOptions, 'input-group input-group-html5' . $size); $style = empty($this->html5Container['style']) ? '' : $this->html5Container['style'] . ';'; if (isset($this->width) && $this->width > 0) { $this->html5Container['style'] = $style . 'width:' . $this->width . ';'; } Html::addCssClass($this->html5Container, 'input-group-addon addon-' . $this->type); $caption = $this->getInput('textInput'); $value = $this->hasModel() ? $this->model[Html::getAttributeName($this->attribute)] : $this->value; $input = Html::input($this->type, $this->html5Options['id'], $value, $this->html5Options); $prepend = static::getAddonContent(ArrayHelper::getValue($this->addon, 'prepend', '')); $append = static::getAddonContent(ArrayHelper::getValue($this->addon, 'append', '')); $preCaption = static::getAddonContent(ArrayHelper::getValue($this->addon, 'preCaption', '')); $prepend .= Html::tag('span', $input, $this->html5Container); $content = Html::tag('div', $prepend . $preCaption . $caption . $append, $this->containerOptions); Html::addCssClass($this->noSupportOptions, 'alert alert-warning'); if ($this->noSupport == false) { $message = ''; } else { $message = "\n<br>" . Html::tag('div', Yii::t('app', $this->noSupport, ['type' => $this->type]), $this->noSupportOptions); } return "<!--[if lt IE 10]>\n{$caption}{$message}\n<![endif]--><![if gt IE 9]>\n{$content}\n<![endif]>"; }
/** * */ public function run() { $view = $this->getView(); $this->registerScript($view); if ($this->hasModel()) { if ($this->label) { $label = $this->label; } else { $label = Html::encode($this->model->getAttributeLabel(Html::getAttributeName($this->attribute))); } $tag = ArrayHelper::remove($this->textOptions, 'tag', 'span'); $text = Html::tag($tag, $label, $this->textOptions); $this->options['label'] = $text; $checkbox = Html::activeCheckbox($this->model, $this->attribute, $this->options); } else { $checkbox = Html::checkbox($this->name, $this->value, $this->options); if ($this->label) { $tag = ArrayHelper::remove($this->textOptions, 'tag', 'span'); $text = Html::tag($tag, $this->label, $this->textOptions); $checkbox = Html::tag('label', $checkbox . ' ' . $text, $this->labelOptions); } } $input = Html::tag('div', $checkbox, $this->containerOptions); echo strtr($this->template, ['{input}' => $input]); }
public function begin() { $attribute = Html::getAttributeName($this->attribute); if ($this->model->hasErrors($attribute)) { $this->inputOptions['class'] .= ' uk-form-danger'; } return parent::begin(); }
/** * @inheritdoc */ public function init() { parent::init(); if (!isset($this->language)) { $this->language = Yii::$app->language; } $this->_lang = $this->getLang($this->language); if ($this->pluginLoading) { $this->_loadIndicator = self::LOAD_PROGRESS; } if ($this->hasModel()) { $this->name = empty($this->options['name']) ? Html::getInputName($this->model, $this->attribute) : $this->options['name']; $this->value = $this->model[Html::getAttributeName($this->attribute)]; } $view = $this->getView(); AssetBundle::register($view); }
/** * Generates a tag that contains the first validation error of the specified model attribute. * Note that even if there is no validation error, this method will still return an empty error tag. * @param \yii\base\Model $model the model object * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format * about attribute expression. * @param array $options the tag options in terms of name-value pairs. The values will be HTML-encoded * using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. * * The following options are specially handled: * * - tag: this specifies the tag name. If not set, "div" will be used. * - encode: boolean, if set to false then value won't be encoded. * * See [[renderTagAttributes()]] for details on how attributes are being rendered. * * @return string the generated label tag */ private function _error($model, $attribute, $options) { $attribute = Html::getAttributeName($attribute); $tag = isset($options['tag']) ? $options['tag'] : 'div'; unset($options['tag']); $options['ng-show'] = 'error.' . $attribute; return Html::tag($tag, Html::tag('div', '{{ error.' . $attribute . ' }}'), $options); }
/** * 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 []; } $options = []; $enableClientValidation = $this->enableClientValidation || $this->enableClientValidation === null && $this->form->enableClientValidation; 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 (!empty($validators)) { $options['validate'] = new JsExpression("function (attribute, value, messages, deferred) {" . implode('', $validators) . '}'); } } $enableAjaxValidation = $this->enableAjaxValidation || $this->enableAjaxValidation === null && $this->form->enableAjaxValidation; if ($enableAjaxValidation) { $options['enableAjaxValidation'] = 1; } if ($enableClientValidation && !empty($options['validate']) || $enableAjaxValidation) { $inputID = Html::getInputId($this->model, $this->attribute); $options['id'] = $inputID; $options['name'] = $this->attribute; foreach (['validateOnChange', 'validateOnType', 'validationDelay'] as $name) { $options[$name] = $this->{$name} === null ? $this->form->{$name} : $this->{$name}; } $options['container'] = isset($this->selectors['container']) ? $this->selectors['container'] : ".field-{$inputID}"; $options['input'] = isset($this->selectors['input']) ? $this->selectors['input'] : "#{$inputID}"; if (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'] !== false; return $options; } else { return []; } }
/** * 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 = $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) . '}'); } // 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']); }
<div class="form-group "> <div class="controls pemember-password-wrap"> <?php echo Html::submitButton(Yii::t('user', 'Sign in'), ['class' => 'btn btn-primary']); ?> <span class="pemember-password text-muted"> <?php echo Html::checkbox(Html::getInputName($model, 'rememberMe'), Html::getAttributeValue($model, "rememberMe"), ["id" => Html::getInputId($model, "rememberMe")]); ?> <label for="<?php echo Html::getInputId($model, "rememberMe"); ?> "><?php echo Html::getAttributeName("rememberMe"); ?> </label> </span> </div> </div> <?php ActiveForm::end(); ?> <div class="ptl mb40"> <?php if ($module->enableConfirmation) { ?> <?php
/** * Retrieves form fields configuration. Fields can be config arrays, ActiveField objects or closures. * * @param \yii\base\Model|\netis\crud\db\ActiveRecord $model * @param array $fields * @param bool $multiple true for multiple values inputs, usually used for search forms * @param array $hiddenAttributes list of attribute names to render as hidden fields * * @return array form fields * @throws InvalidConfigException */ public static function getFormFields($model, $fields, $multiple = false, $hiddenAttributes = []) { if (!$model instanceof \yii\db\ActiveRecord) { return $model->safeAttributes(); } $keys = Action::getModelKeys($model); $hiddenAttributes = array_flip($hiddenAttributes); list($behaviorAttributes, $blameableAttributes) = Action::getModelBehaviorAttributes($model); $attributes = $model->safeAttributes(); $relations = $model->relations(); if (($versionAttribute = $model->optimisticLock()) !== null) { $hiddenAttributes[$versionAttribute] = true; } $formFields = []; foreach ($fields as $key => $field) { if ($field instanceof ActiveField) { $formFields[$key] = $field; continue; } elseif (!is_string($field) && is_callable($field)) { $formFields[$key] = call_user_func($field, $model); if (!is_string($formFields[$key])) { throw new InvalidConfigException('Field definition must be either an ActiveField or a callable.'); } continue; } elseif (!is_string($field)) { throw new InvalidConfigException('Field definition must be either an ActiveField or a callable.'); } $attributeName = Html::getAttributeName($field); if (in_array($attributeName, $relations)) { $formFields = static::addRelationField($formFields, $model, $field, $hiddenAttributes, $attributes, $multiple); } elseif (in_array($attributeName, $attributes)) { if (in_array($attributeName, $keys) || in_array($attributeName, $behaviorAttributes)) { continue; } $formFields = static::addFormField($formFields, $model, $field, $hiddenAttributes, $multiple); } } return $formFields; }
/** * @param string|null $label the label or null to use model label * @param array $options the tag options */ protected function renderLabelParts($label = null, $options = []) { $options = array_merge($this->labelOptions, $options); if ($label === null) { if (isset($options['label'])) { $label = $options['label']; unset($options['label']); } else { $attribute = Html::getAttributeName($this->attribute); $label = Html::encode($this->model->getAttributeLabel($attribute)); } } $this->parts['{beginLabel}'] = Html::beginTag('label', $options); $this->parts['{endLabel}'] = Html::endTag('label'); $this->parts['{labelTitle}'] = $label; }
/** * Renders switcher * @see http://materializecss.com/forms.html#switches * @param array $options * @param array $flags * @return $this */ public function switcher($options = [], $flags = null) { parent::checkbox($options, false); if ($flags === null) { $label = Html::encode($this->model->getAttributeLabel(Html::getAttributeName($this->attribute))); $labelParts = explode(',', $label); $flags = count($labelParts) >= 2 ? $labelParts : null; } if ($flags) { Html::removeCssClass($this->options, 'input-field'); Html::addCssClass($this->options, 'switch'); $labelContent = $flags[0] . $this->parts['{input}'] . Html::tag('span', '', ['class' => 'lever']) . $flags[1]; $this->parts['{input}'] = Html::label($labelContent, Html::getInputId($this->model, $this->attribute)); } return $this; }
/** * Initializes placeholder based on $autoPlaceholder * * @param array $options the HTML attributes for the input */ protected function initPlaceholder(&$options) { if ($this->autoPlaceholder) { $label = $this->model->getAttributeLabel(Html::getAttributeName($this->attribute)); $this->inputOptions['placeholder'] = $label; $options['placeholder'] = $label; } }
/** * Renders a static input (display only). * * @param array $options the tag options in terms of name-value pairs. * @return ActiveField object */ public function staticInput($options = []) { if ($options && ArrayHelper::keyExists('value', $options)) { $content = $options['value']; unset($options['value']); } else { $content = isset($this->model[Html::getAttributeName($this->attribute)]) ? $this->model[Html::getAttributeName($this->attribute)] : '-'; } Html::addCssClass($options, 'form-control-static'); $this->parts['{input}'] = Html::tag('p', $content, $options); return $this; }
/** * Add the maxlength attribute to an ActiveField. * * The plugin requires that the max number of characters is specified as the HTML5 attribute "maxlength". This * method adds this attribute if it is not already defined into the HTML attributes of an ActiveField. The * value is retrieved from the StringValidator settings that is attached to the model attribute. * Note that if maxlength can't be defined, the plugin is not registred for the view. * * Note that this method is deprecated and should be replaced by ActiveField widget initialization * as explained in the README file. * * @deprecated since Yii2 2.0.3 it is possible to set the maxlength HTML attribute of a text input * [Read more](http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#textInput%28%29-detail) * * @param yii\widgets\ActiveField $field * @param array $clientOptions Bootstrap maxlength plugin options * @param boolean $render when true, the $field is output * @return yii\widgets\ActiveField the field containing the "maxlength" option (if it could be obtained) */ public static function apply($field, $clientOptions, $render = true) { if (isset($field->inputOptions['maxlength'])) { $maxLength = $field->inputOptions['maxlength']; } else { $maxLength = static::getMaxLength($field->model, Html::getAttributeName($field->attribute)); } if (!empty($maxLength)) { $field->inputOptions['maxlength'] = $maxLength; $id = Html::getInputId($field->model, $field->attribute); static::widget(['selector' => '#' . $id, 'clientOptions' => $clientOptions]); } if ($render) { echo $field; } return $field; }
/** * @see \yii\widgets\ActiveField::begin() */ public function begin() { if ($this->form->enableClientScript) { $clientOptions = $this->getClientOptions(); if (!empty($clientOptions)) { $this->form->attributes[] = $clientOptions; } } $inputID = Html::getInputId($this->model, $this->attribute); $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::getValue($this->options, 'tag', 'div'); //$tag = isset($this->options['tag']) ? $this->options['tag'] : 'div'; if ($tag) { return Html::beginTag($tag, $options); } }
/** * If the model attribute is validated by a string validator, * the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. * @param Model $model the model object * @param string $attribute the attribute name or expression. */ protected function normalizeMaxLength() { if (isset($this->options['maxlength']) && $this->options['maxlength'] !== true) { return; } unset($this->options['maxlength']); if (!$this->hasModel()) { return; } $attrName = Html::getAttributeName($this->attribute); foreach ($this->model->getActiveValidators($attrName) as $validator) { if ($validator instanceof StringValidator && $validator->max !== null) { if ($validator->when === null || call_user_func($validator->when, $this->model, $this->attribute)) { $this->options['maxlength'] = $validator->max; return; } } } return; }
/** * @return string */ protected function getLabelContent() { $label = array_key_exists('label', $this->options) ? $this->options['label'] : ''; if ($this->hasModel() && empty($label)) { $label = Html::encode($this->model->getAttributeLabel(Html::getAttributeName($this->attribute))); } $this->options['label'] = null; return $label; }