A form field is associated with a model and an attribute. It contains a label, an input and an error message
and use them to interact with end users to collect their inputs for the attribute.
public field ( |
||
$model | the data model. | |
$attribute | string | the attribute name or expression. See [[Html::getAttributeName()]] for the format about attribute expression. |
$options | array | the additional configurations for the field object. These are properties of [[ActiveField]] or a subclass, depending on the value of [[fieldClass]]. |
리턴 | the created ActiveField object. |
/** * Формирование Html кода поля для вывода в форме * @param ActiveForm $form объект форма * @param array $options массив html атрибутов поля * @param bool|int $index инднкс модели при табличном вводе * @return string */ public function renderInput(ActiveForm $form, array $options = [], $index = false) { $options = ArrayHelper::merge($this->options, $options); $attr = $this->modelField->attr; $defaults = ["maxFileSize" => $this->modelField->model->maxFileSize, "uploadRoute" => $this->defaultRoute]; $widgetOptions = ArrayHelper::merge($defaults, $this->widgetOptions, ["options" => $options]); $attr = $this->getFormAttrName($index, $attr); return $form->field($this->modelField->model, $attr)->widget(Html5Widget::className(), $widgetOptions); }
/** * Button with multiple submit options. * @param ActiveForm $form * @param $model * @param $attribute * @param $items * @param string $content * @param array $options * @return string */ public static function submitDropdown(ActiveForm $form, $model, $attribute, $items, $content = "Save", $options = []) { if (count($items) == 1) { return $form->field($model, $attribute, ['options' => ['class' => 'pull-left']])->label(false)->hiddenInput(['value' => key($items)]) . Html::submitButton(reset($items), $options); } $lis = ''; foreach ($items as $key => $name) { $lis .= Html::tag('li', "<a href='#'>{$name}</a>", ['onclick' => "\$(this).closest('div').find('input').val('{$key}');\n \$(this).parents('form').submit();", 'class' => $model->{$attribute} == $key ? 'active' : '']); } return Html::beginTag('div', ['class' => 'btn-group']) . $form->field($model, $attribute, ['options' => ['class' => 'pull-left']])->label(false)->hiddenInput() . Html::button($content . '<span class="caret"></span>', ['class' => 'btn dropdown-toggle btn-primary', "data-toggle" => "dropdown"]) . Html::beginTag('ul', ['class' => 'dropdown-menu']) . $lis . Html::endTag('ul') . Html::endTag('div'); }
/** * @inheritdoc */ public function run() { parent::init(); ListInputAsset::register($this->view); $pluginOptions = Json::encode($this->pluginOptions); $this->view->registerJs(sprintf('$("#%s").listinput(%s)', $this->options['id'], $pluginOptions)); if ($this->hasModel()) { if ($this->form == null) { throw new InvalidConfigException(__CLASS__ . '::$form must be specifed.'); } return $this->form->field($this->model, $this->attribute, ['template' => $this->template])->textInput($this->options); } else { return Html::textInput($this->name, $this->value, $this->options); } }
/** * Формирование Html кода поля для вывода в форме * @param ActiveForm $form объект форма * @param array $options массив html атрибутов поля * @param bool|int $index инднкс модели при табличном вводе * @return string */ public function renderInput(ActiveForm $form, array $options = [], $index = false) { $options = ArrayHelper::merge($this->options, $options); $widgetOptions = ArrayHelper::merge(["visibleOptions" => ["class" => "form-control"]], $this->widgetOptions, ["options" => $options]); $attr = $this->getFormAttrName($index, $this->modelField->attr); return $form->field($this->modelField->model, $attr)->widget(AutoComplete::className(), $widgetOptions); }
public function renderConfigForm(ActiveForm $activeForm) { echo $activeForm->field($this, 'isLive')->checkbox(); echo $activeForm->field($this, 'sMerchantLogin')->textInput(); echo $activeForm->field($this, 'sMerchantPass1')->textInput(); echo $activeForm->field($this, 'sMerchantPass2')->textInput(); }
/** * Формирование Html кода поля для вывода в форме * @param ActiveForm $form объект форма * @param array $options массив html атрибутов поля * @param bool|int $index инднкс модели при табличном вводе * @return string */ public function renderInput(ActiveForm $form, array $options = [], $index = false) { $options = ArrayHelper::merge($this->options, $options); $widgetOptions = ArrayHelper::merge($this->widgetOptions, ["options" => $options]); $attr = $this->getFormAttrName($index, $this->modelField->attr); return $form->field($this->modelField->model, $attr)->widget(MarkItUp::className(), $widgetOptions); }
/** * @inheritdoc */ public function getExtendedFilterForm(ActiveForm $form, array $options = []) { $data = $this->defaultGridFilter(); if (!isset($options['prompt'])) { $options['prompt'] = ''; } return $form->field($this->model, $this->attr)->dropDownList($data, $options); }
public function testBooleanAttributes() { $o = ['template' => '{input}']; $model = new DynamicModel(['name']); ob_start(); $form = new ActiveForm(['action' => '/something']); ob_end_clean(); $this->assertEquals(<<<EOF <div class="form-group field-dynamicmodel-name"> <input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]" required> </div> EOF , (string) $form->field($model, 'name', $o)->input('email', ['required' => true])); $this->assertEquals(<<<EOF <div class="form-group field-dynamicmodel-name"> <input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]"> </div> EOF , (string) $form->field($model, 'name', $o)->input('email', ['required' => false])); $this->assertEquals(<<<EOF <div class="form-group field-dynamicmodel-name"> <input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]" required="test"> </div> EOF , (string) $form->field($model, 'name', $o)->input('email', ['required' => 'test'])); }
/** * @inheritdoc */ public function run() { $id = Html::getInputId($this->model, 'image_deleted'); $options = ['options' => ['accept' => 'image/*'], 'pluginOptions' => ['showUpload' => false], 'pluginEvents' => ['fileclear' => "function() { \$('#{$id}').val(1);}"]]; if ($this->model->imageUrl !== null) { $options['pluginOptions']['initialPreview'] = [Html::img($this->model->imageUrl, ['class' => 'file-preview-image'])]; } echo $this->form->field($this->model, 'image_deleted', ['template' => '{input}'])->hiddenInput(); echo $this->form->field($this->model, 'image')->widget(FileInput::classname(), $options); }
/** * Формирование Html кода поля для вывода в форме * @param ActiveForm $form объект форма * @param array $options массив html атрибутов поля * @param bool|int $index инднкс модели при табличном вводе * @return string */ public function renderInput(ActiveForm $form, array $options = [], $index = false) { $options = ArrayHelper::merge($this->options, $options); $widgetOptions = ArrayHelper::merge(["options" => ["class" => "form-control"]], $this->widgetOptions, ["options" => $options]); $fieldOptions = ["options" => ["class" => "form-group col-xs-6"]]; $html = Html::beginTag('div', ['class' => 'row']); $html .= $form->field($this->modelField->model, $this->fromAttr, $fieldOptions)->widget(DatePicker::className(), $widgetOptions); $html .= $form->field($this->modelField->model, $this->toAttr, $fieldOptions)->widget(DatePicker::className(), $widgetOptions); $html .= Html::endTag('div'); return $html; }
/** * Формирование Html кода поля для вывода в форме * @param ActiveForm $form объект форма * @param array $options массив html атрибутов поля * @param bool|int $index инднкс модели при табличном вводе * @return string */ public function renderInput(ActiveForm $form, array $options = [], $index = false) { $data = $this->modelField->getDataValue(); if (empty($data)) { return false; } $options = ArrayHelper::merge($this->options, $options, ["multiple" => true]); $widgetOptions = ArrayHelper::merge(["data" => $data], $this->widgetOptions, ["options" => $options]); $attr = $this->getFormAttrName($index, $this->modelField->attr); return $form->field($this->modelField->model, $attr)->widget(MultiSelect::className(), $widgetOptions); }
public function renderConfigForm(ActiveForm $form) { echo $form->fieldSet(\Yii::t('skeeks/seo', 'Keywords')); echo $form->field($this, 'kladrApiToken'); echo $form->field($this, 'kladrRequestLimit'); echo $form->field($this, 'russiaId')->listBox(\yii\helpers\ArrayHelper::map(\skeeks\cms\kladr\models\KladrLocation::find()->where(['type' => \skeeks\cms\kladr\models\KladrLocation::TYPE_COUNTRY])->all(), 'id', 'name'), ['size' => 1]); echo $form->fieldSetEnd(); }
/** * @inheritdoc */ public function run() { Asset::register($this->view); $config = empty($this->config) ? json_encode(['locale' => \Yii::$app->language]) : json_encode($this->config); $this->view->registerJs(sprintf('$("#%s").passField(%s)', $this->options['id'], $config)); if ($this->hasModel()) { if ($this->form == null) { throw new InvalidConfigException(__CLASS__ . '.form property must be specified'); } return $this->form->field($this->model, $this->attribute)->passwordInput($this->options); } else { return Html::passwordInput($this->name, $this->value, $this->options); } }
public function renderConfigForm(ActiveForm $form) { echo $form->fieldSet(\Yii::t('skeeks/cms', 'Main')); echo $form->field($this, 'enabled')->checkbox(); echo $form->fieldCheckboxBoolean($this, 'isOpen'); echo $form->field($this, 'enableFancyboxWindow')->widget(\skeeks\widget\chosen\Chosen::className(), ['items' => \Yii::$app->formatter->booleanFormat]); echo $form->fieldRadioListBoolean($this, 'editWidgets'); echo $form->fieldRadioListBoolean($this, 'editViewFiles'); echo $form->field($this, 'infoblockEditBorderColor')->widget(\skeeks\cms\widgets\ColorInput::className()); echo $form->fieldSetEnd(); echo $form->fieldSet(\Yii::t('skeeks/cms', 'Access')); echo \skeeks\cms\widgets\rbac\PermissionForRoles::widget(['permissionName' => \skeeks\cms\rbac\CmsManager::PERMISSION_CONTROLL_PANEL, 'label' => 'Доступ к панеле разрешен']); echo $form->fieldSetEnd(); }
public function run() { echo $this->form->field($this->model, $this->model->getCountryPropertyName())->dropDownList(ArrayHelper::map(Country::find()->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), ['id' => 'location_country_id', 'prompt' => Yii::t('common/geo/country', 'Select country')]); if ($this->localized === $this->model->getCountryPropertyName()) { return; } echo $this->form->field($this->model, $this->model->getRegionPropertyName())->widget(DepDrop::className(), ['options' => ['id' => 'location_region_id', 'placeholder' => Yii::t('common/geo/region', 'Select region')], 'data' => ArrayHelper::map(Region::find()->where(['country_id' => $this->model->country_id])->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), 'pluginOptions' => ['url' => Url::to(['/geo/region/list']), 'depends' => ['location_country_id']]]); if ($this->localized === $this->model->getRegionPropertyName()) { return; } echo $this->form->field($this->model, $this->model->getCityPropertyName())->widget(DepDrop::className(), ['options' => ['id' => 'location_city_id', 'cityholder' => Yii::t('common/geo/city', 'Select city')], 'data' => ArrayHelper::map(City::find()->where(['region_id' => $this->model->region_id])->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), 'pluginOptions' => ['url' => Url::to(['/geo/city/list']), 'depends' => ['location_region_id']]]); if ($this->localized === $this->model->getCityPropertyName()) { return; } echo $this->form->field($this->model, 'address')->textInput(); }
/** * 重写输入框 * @param type $model * @param type $attribute * @param type $options * @return type */ public function field($model, $attribute, $options = array()) { if (empty($options)) { $options = ['template' => '{label}<div class="col-sm-5">{input}</div><div class="col-sm-5">{error}</div>', 'labelOptions' => ['class' => 'col-sm-2 control-label']]; } return parent::field($model, $attribute, $options); }
/** * @inheritdoc */ public function run() { parent::init(); if (!isset($this->pluginOptions['monthNames'])) { $this->pluginOptions = ArrayHelper::merge($this->pluginOptions, ['monthNames' => $this->getMonthNames()]); } if (!isset($this->pluginOptions['template'])) { $this->pluginOptions = ArrayHelper::merge($this->pluginOptions, ['template' => $this->getTemplate()]); } BirthdayPickerAsset::register($this->view); $pluginOptions = Json::encode($this->pluginOptions); $this->view->registerJs(sprintf('$("#%s").birthdayPicker(%s)', $this->options['id'], $pluginOptions)); if ($this->hasModel()) { if ($this->form == null) { throw new InvalidConfigException(__CLASS__ . '::$form must be specifed.'); } return $this->form->field($this->model, $this->attribute, ['template' => $this->template])->textInput($this->options); } else { return Html::textInput($this->name, $this->value, $this->options); } }
/** * @inheritdoc */ public function run() { parent::init(); PassfieldAsset::register($this->view); Yii::$app->get('i18n')->translations['passfield*'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages']; if (ArrayHelper::getValue($this->pluginOptions, 'titleShow') === null) { $this->pluginOptions['titleShow'] = Yii::t('passfield', 'Show password'); } if (ArrayHelper::getValue($this->pluginOptions, 'titleHide') === null) { $this->pluginOptions['titleHide'] = Yii::t('passfield', 'Hide password'); } $pluginOptions = Json::encode($this->pluginOptions); $this->view->registerJs(sprintf('$("#%s").passfield(%s)', $this->options['id'], $pluginOptions)); if ($this->hasModel()) { if ($this->form == null) { throw new InvalidConfigException(__CLASS__ . '.form property must be specified'); } return $this->form->field($this->model, $this->attribute, ['template' => $this->template])->passwordInput($this->options)->hint($this->hint); } else { return Html::passwordInput($this->name, $this->value, $this->options); } }
/** * */ private function _setupPasswordFields() { if (!$this->forcePasswordGeneration) { if ($this->enablePasswordGeneration) { echo $this->form->field($this->model, 'generatePassword')->checkBox(['attribute' => 'generatePassword', 'id' => 'generate-password', 'onclick' => "this.checked ? \$('#password-fields').hide() : \$('#password-fields').show()", 'format' => 'boolean']); } echo '<div id="password-fields" style="display:'; if ($this->model->generatePassword) { echo 'none"'; } else { echo 'block"'; } echo '>'; echo $this->form->field($this->model, 'password')->input('password')->hint('Password should be within A-Za-z0-9')->label('Password'); echo $this->form->field($this->model, 'password_confirm')->input('password')->label('Confirm Password'); echo '</div>'; } }
public function testIssue5356() { $o = ['template' => '{input}']; $model = new DynamicModel(['categories']); $model->categories = 1; ob_start(); $form = new ActiveForm(['action' => '/something']); ob_end_clean(); // https://github.com/yiisoft/yii2/issues/5356 $this->assertEqualsWithoutLE(<<<EOF <div class="form-group field-dynamicmodel-categories"> <input type="hidden" name="DynamicModel[categories]" value=""><select id="dynamicmodel-categories" class="form-control" name="DynamicModel[categories][]" multiple size="4"> <option value="0">apple</option> <option value="1" selected>banana</option> <option value="2">avocado</option> </select> </div> EOF , (string) $form->field($model, 'categories', $o)->listBox(['apple', 'banana', 'avocado'], ['multiple' => true])); }
public function renderConfigForm(ActiveForm $activeForm) { echo $activeForm->field($this, 'isLive')->checkbox(); echo $activeForm->field($this, 'shop_password'); echo $activeForm->field($this, 'security_type'); echo $activeForm->field($this, 'shop_id'); echo $activeForm->field($this, 'scid'); echo $activeForm->field($this, 'payment_type'); echo Alert::widget(['options' => ['class' => 'alert-info'], 'body' => <<<HTML <a target="_blank" href="https://tech.yandex.ru/money/doc/payment-solution/shop-config/intro-docpage/">Подключение магазина</a><br /> В настройках вашего магазина на yandex укажите: <br /> Укажите checkUrl: /shop/yandex-kassa/check-order<br /> Укажите avisoUrl: /shop/yandex-kassa/payment-aviso<br /> <hr /> <a target="_blank" href="https://tech.yandex.ru/money/doc/payment-solution/examples/examples-test-data-docpage/">Тестовые данные</a><br /> HTML ]); }
/** * Drawing form element * @return \yii\widgets\ActiveField */ public function renderForActiveForm() { $field = $this->activeForm->field($this->property->relatedPropertiesModel, $this->property->code); if (!$field) { return ''; } return $field; }
protected function renderRow($index, $value) { if (!empty($this->rowView)) { $row = $this->render($this->rowView, ['index' => $index, 'value' => $value, 'form' => $this->form, 'model' => $this->model, 'attribute' => $this->attribute]); } else { $options = $this->options; Html::addCssClass($options['options'], 'col-lg-11'); $row = $this->form->field($this->model, "{$this->attribute}[{$index}]", $options); } $button = $this->renderButton($index); return Html::tag('div', $row . $button, ['class' => 'row']); }
/** * Формирование Html кода поля для вывода в форме * @param ActiveForm $form объект форма * @param array $options массив html атрибутов поля * @param bool|int $index инднкс модели при табличном вводе * @return string */ public function renderInput(ActiveForm $form, array $options = [], $index = false) { $options = ArrayHelper::merge($this->options, $options); $widgetOptions = ArrayHelper::merge(["options" => ["class" => "form-control", "prompt" => ""], "data" => $this->modelField->getDataValue()], $this->widgetOptions, ["options" => $options]); $attr = $this->modelField->attr; return $form->field($this->modelField->model, $this->getFormAttrName($index, $attr))->widget(DependDropDown::className(), $widgetOptions); }
public function field($model, $attribute, $options = []) { if ($this->language) { return parent::field($model, $attribute . '_' . $this->language, $options); } return parent::field($model, $attribute, $options); }
/** * @return string */ public function renderConfigForm(ActiveForm $activeForm) { echo $activeForm->fieldRadioListBoolean($this, 'showDefaultPalette'); echo $activeForm->fieldRadioListBoolean($this, 'useNative'); echo $activeForm->fieldRadioListBoolean($this, 'showInput')->hint(\Yii::t('skeeks/cms', 'This INPUT to opened the palette')); echo $activeForm->fieldRadioListBoolean($this, 'showAlpha'); echo $activeForm->fieldRadioListBoolean($this, 'showPalette'); echo $activeForm->field($this, 'saveValueAs')->radioList(\skeeks\cms\widgets\ColorInput::$possibleSaveAs); }
public function run() { $hours = []; for ($i = 0; $i < 25; $i++) { $hours[$i] = $i < 10 ? '0' . $i : $i; } $minutes = []; for ($i = 0; $i < 60; $i++) { $minutes[$i] = $i < 10 ? '0' . $i : $i; } $content = ''; $content .= Html::beginTag('div', ['id' => $this->getId()]); $content .= Html::beginTag('div', ['class' => 'control']); $content .= $this->renderButtons(); $content .= Html::beginTag('div', ['class' => 'opening-hours-block time']); $content .= Html::beginTag('div', ['class' => 'inputs']); $content .= Html::tag('span', '', ['class' => 'glyphicon glyphicon-time']); $content .= Html::dropDownList('work-from-hours', null, $hours, ['class' => 'form-control work-from-hours']) . ' : '; $content .= Html::dropDownList('work-from-minutes', null, $minutes, ['class' => 'form-control work-from-minutes']) . ' — '; $content .= Html::dropDownList('work-to-hours', null, $hours, ['class' => 'form-control work-to-hours']) . ' : '; $content .= Html::dropDownList('work-to-minutes', null, $minutes, ['class' => 'form-control work-to-minutes']); $content .= Html::endTag('div'); $content .= Html::beginTag('div', ['class' => 'inputs timeout-block', 'style' => 'display:none']); $content .= Html::tag('span', '', ['class' => 'glyphicon glyphicon-cutlery']); $content .= Html::dropDownList('timeout-from-hours', null, $hours, ['class' => 'form-control timeout-from-hours']) . ' : '; $content .= Html::dropDownList('timeout-from-minutes', null, $minutes, ['class' => 'form-control timeout-from-minutes']) . ' — '; $content .= Html::dropDownList('timeout-to-hours', null, $hours, ['class' => 'form-control timeout-to-hours']) . ' : '; $content .= Html::dropDownList('timeout-to-minutes', null, $minutes, ['class' => 'form-control timeout-to-minutes']); $content .= Html::endTag('div'); $content .= Html::a('круглосуточно', '#', ['class' => 'dotted alltime-btn']); $content .= Html::a('перерыв', '#', ['class' => 'dotted timeout-btn']); $content .= Html::tag('div', '', ['class' => 'shirma']); $content .= Html::endTag('div'); $content .= Html::a(Html::tag('span', '', ['class' => 'glyphicon glyphicon-plus-sign']), '#', ['class' => 'link add-control-btn']); $content .= Html::endTag('div'); foreach ($this->schedule->getDays() as $day) { $content .= $this->form->field($this->schedule, "{$day}[0][from][hours]", ['options' => ['class' => null]])->hiddenInput()->label(false); $content .= $this->form->field($this->schedule, "{$day}[0][from][minutes]", ['options' => ['class' => null]])->hiddenInput()->label(false); $content .= $this->form->field($this->schedule, "{$day}[0][to][hours]", ['options' => ['class' => null]])->hiddenInput()->label(false); $content .= $this->form->field($this->schedule, "{$day}[0][to][minutes]", ['options' => ['class' => null]])->hiddenInput()->label(false); $content .= $this->form->field($this->schedule, "{$day}[1][from][hours]", ['options' => ['class' => null]])->hiddenInput()->label(false); $content .= $this->form->field($this->schedule, "{$day}[1][from][minutes]", ['options' => ['class' => null]])->hiddenInput()->label(false); $content .= $this->form->field($this->schedule, "{$day}[1][to][hours]", ['options' => ['class' => null]])->hiddenInput()->label(false); $content .= $this->form->field($this->schedule, "{$day}[1][to][minutes]", ['options' => ['class' => null]])->hiddenInput()->label(false); } $content .= Html::endTag('div'); return $content; }
/** @test */ public function it_gives_the_errors_to_active_form_normally() { ob_start(); ob_implicit_flush(false); $form = new ActiveForm(['action' => '/something']); $order = new Order(); $order->validate(); echo $form->field($order, 'address'); $content = ob_get_clean(); $this->assertContains('Address cannot be blank', $content); }
/** * Формирование Html кода поля для вывода в форме * @param ActiveForm $form объект форма * @param array $options массив html атрибутов поля * @param bool|int $index инднкс модели при табличном вводе * @return string */ public function renderInput(ActiveForm $form, array $options = [], $index = false) { $data = $this->modelField->getDataValue(); if (empty($data)) { return false; } $options = ArrayHelper::merge(["class" => "form-control"], $this->options, $options, ["multiple" => true]); $widgetOptions = ArrayHelper::merge(["items" => $data, "bootstrap" => true, "settings" => ["tokenSeparators" => [',', ' ']]], $this->widgetOptions, ["options" => $options]); $attr = $this->getFormAttrName($index, $this->modelField->attr); return $form->field($this->modelField->model, $attr)->widget(SortedTags::className(), $widgetOptions); }
public function renderConfigForm(ActiveForm $form) { echo $form->fieldSet(\Yii::t('app', 'Main')); echo $form->fieldSelect($this, 'themeColor', static::themes(), ['allowDeselect' => true]); echo $form->fieldRadioListBoolean($this, 'boxedLayout'); echo $form->field($this, 'boxedBgImage')->widget(\skeeks\cms\modules\admin\widgets\formInputs\OneImage::className()); echo $form->field($this, 'boxedBgCss')->textInput()->hint('repeat or fixed center center'); echo $form->fieldSetEnd(); }