Yii2: make checkbox to be checked
in a view which uses ActiveForm., Explore Collectives , Collectives ,I guess this checkbox will be checked only if $model->order property take true value and if it has false (0 or null or false etc) value — field will be unchecked.
if your are setting an external value in checkbox.
order = "02256"; ?> field($model, "order")->checkbox(['value' => "02256"]); ?>
Answer by Paxton Grant
Thanks! Now I see that I mistakenly used ActiveForm instead of Html helper to generate checkbox. For the sake of simplicity, I omitted some details in my question. One of them was that the checkbox was related not to a property of the model but to a property of an associated model. For this reason it is better to use Html helper than ActiveForm.,Refer the API docs. The active field checkbox will generate the «checked» tag attribute according to the model attribute value. So you simply set the default value of the model attribute, to true before you render your view.,So, how I can generate html code for a checkbox with attribute «checked»?,I feel myself a little crazy, but I can not manage to make a checkbox to be checked. I’d like to generate an html code like this
I feel myself a little crazy, but I can not manage to make a checkbox to be checked. I’d like to generate an html code like this
echo $form->field($model, 'order')->checkBox(['label' => . 'uncheck' => null, 'checked' => true]);
echo $form->field($model, 'order')->checkBox(['label' => . 'uncheck' => null, 'checked' => 'checked']);
Strangely enough, if I substitute «checked» with «selected»
echo $form->field($model, 'order')->checkBox(['label' => . 'uncheck' => null, 'selected' => true]);
then generated html code contains attribute «selected»:
Answer by Annalise Woodward
I expect to see the checkbox checked.,The checked option is ignored.,but to fix this issue we could set checked option only if it does not exist already here: ,this must be checked by default render but not working.
field($model, 'categories[]', ['options' => ['class' => 'xform-group']])->checkbox([ 'uncheck' => null, 'class' => 'chk', 'id' => 'model-category_id_' . $category->id, 'label' => $category->name, 'labelOptions' => ['class' => 'chk full-width'], 'template' => '', 'value' => $category->id, 'checked' => true, ]);?>
Answer by Cillian Brewer
null: Checkbox is indeterminate.,0: Checkbox is unchecked.,1: Checkbox is checked.,You can set the plugin to allow three states or the default two states for the checkbox.
Generally, you may also want to set a default version globally for all Krajee Extensions (instead of setting it for each widget or extension separately). In order to do this, you can setup the bsVersion property within Yii 2 application params (i.e. Yii::$app->params[‘bsVersion’] ). To set this up, add this section of code to your application params configuration file (e.g. config/params.php ):
Answer by Daxton Doyle
public function actionForm() < echo Html::beginForm(); echo Html::checkboxList('test', ['value 1' =>'item 1', 'value 2' => 'item 2', 'value 3' => 'item 3'], isset($_POST['test']) ? $_POST['test'] : null, function ($index, $label, $name, $value, $checked) < return Html::label($label . ' ' . Html::checkbox($name, $value, $checked), null, ['class' =>'inline checkbox']); >); echo Html::submitButton(); echo Html::endForm(); print_r($_POST); >
Answer by Paxton Magana
0How to use the application classes in the test?,1How to use Yii2-images upload an image at the time of publication?,Find more questions by tags Yii,0XBox One Fallout 4 and Mortal Kombat XL crash when connecting to the Internet, what to do?
Answer by Ryland Roberson
I guess this checkbox will be checked only if $model->order property take true value and if it has false (0 or null or false etc) value — field will be unchecked.,if your are setting an external value in checkbox.
if your are setting an external value in checkbox.
order = "02256"; ?> field($model, "order")->checkbox(['value' => "02256"]); ?>
Answer by Reagan Nixon
How can I make the check list is checked if the value is exist in table ?,Let say, the ID_REQUEST = 2, then /index.php?r=it%2Frequest%2Fupdate&id=1 Then, the all checkbox will be checked.,I interprated them into checklist. this is the view of update :,Note** this option does not delete the question immediately,Since others contribution also matters and security reasons.Your request will be Queued.We will review the question and remove.It may take some days.
table request
mysql> select ID_REQUEST, NOMOR_SURAT, KELUHAN, DITERIMA_OLEH FROM request; +------------+-------------+---------+---------------+ | ID_REQUEST | NOMOR_SURAT | KELUHAN | DITERIMA_OLEH | +------------+-------------+---------+---------------+ | 1 | 1 | a | Dzil | | 2 | 2 | A | Pealariannya | +------------+-------------+---------+---------------+ 2 rows in set (0.00 sec)
table tipe_request
mysql> SELECT * FROM tipe_request; +---------+------------+ | ID_TIPE | NAMA_TIPE | +---------+------------+ | 1 | Perbaikan | | 2 | Permintaan | +---------+------------+ 2 rows in set (0.00 sec) mysql>
table link_req_type
mysql> select * from link_req_tipe; +---------+------------+---------+ | ID_LINK | ID_REQUEST | ID_TIPE | +---------+------------+---------+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 2 | 2 | +---------+------------+---------+ 3 rows in set (0.00 sec) mysql>
The problem is, I have a form that have aim to update the table link_req_type
public function actionUpdate($id) < $model = $this->findModel($id); $tipeRequest = new LinkReqTipe(); if ($model->load(Yii::$app->request->post()) && $model->save()) < return $this->redirect(['view', 'id' => $model->ID_REQUEST]); > else < return $this->render('update', [ 'model' => $model, 'tipeRequest' => $tipeRequest::find()->where(['ID_REQUEST' => $id])->all(), ]); > >
I interprated them into checklist. this is the view of update :
title = 'Update Request: ' . $model->ID_REQUEST; $this->params['breadcrumbs'][] = ['label' => 'Requests', 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => $model->ID_REQUEST, 'url' => ['view', 'id' => $model->ID_REQUEST]]; $this->params['breadcrumbs'][] = 'Update'; ?> Coz, my code now is like this in _form.php
Class yii\helpers\Html
Nearly all of the methods in this class allow setting additional html attributes for the html tags they generate. You can specify, for example, class , style or id for an html element using the $options parameter. See the documentation of the tag() method for more details.
For more details and usage information on Html, see the guide article on html helpers.
Public Properties
Property Type Description Defined By $attributeOrder array The preferred order of attributes in a tag. yii\helpers\BaseHtml $attributeRegex string Regular expression used for attribute name validation. yii\helpers\BaseHtml $dataAttributes array List of tag attributes that should be specially handled when their values are of array type. yii\helpers\BaseHtml $normalizeClassAttribute boolean Whether to removes duplicate class names in tag attribute class yii\helpers\BaseHtml $voidElements array List of void elements (element name => 1) yii\helpers\BaseHtml
Public Methods
Method Description Defined By a() Generates a hyperlink tag. yii\helpers\BaseHtml activeCheckbox() Generates a checkbox tag together with a label for the given model attribute. yii\helpers\BaseHtml activeCheckboxList() Generates a list of checkboxes. yii\helpers\BaseHtml activeDropDownList() Generates a drop-down list for the given model attribute. yii\helpers\BaseHtml activeFileInput() Generates a file input tag for the given model attribute. yii\helpers\BaseHtml activeHiddenInput() Generates a hidden input tag for the given model attribute. yii\helpers\BaseHtml activeHint() Generates a hint tag for the given model attribute. yii\helpers\BaseHtml activeInput() Generates an input tag for the given model attribute. yii\helpers\BaseHtml activeLabel() Generates a label tag for the given model attribute. yii\helpers\BaseHtml activeListBox() Generates a list box. yii\helpers\BaseHtml activePasswordInput() Generates a password input tag for the given model attribute. yii\helpers\BaseHtml activeRadio() Generates a radio button tag together with a label for the given model attribute. yii\helpers\BaseHtml activeRadioList() Generates a list of radio buttons. yii\helpers\BaseHtml activeTextInput() Generates a text input tag for the given model attribute. yii\helpers\BaseHtml activeTextarea() Generates a textarea tag for the given model attribute. yii\helpers\BaseHtml addCssClass() Adds a CSS class (or several classes) to the specified options. yii\helpers\BaseHtml addCssStyle() Adds the specified CSS style to the HTML options. yii\helpers\BaseHtml beginForm() Generates a form start tag. yii\helpers\BaseHtml beginTag() Generates a start tag. yii\helpers\BaseHtml button() Generates a button tag. yii\helpers\BaseHtml buttonInput() Generates an input button. yii\helpers\BaseHtml checkbox() Generates a checkbox input. yii\helpers\BaseHtml checkboxList() Generates a list of checkboxes. yii\helpers\BaseHtml csrfMetaTags() Generates the meta tags containing CSRF token information. yii\helpers\BaseHtml cssFile() Generates a link tag that refers to an external CSS file. yii\helpers\BaseHtml cssStyleFromArray() Converts a CSS style array into a string representation. yii\helpers\BaseHtml cssStyleToArray() Converts a CSS style string into an array representation. yii\helpers\BaseHtml decode() Decodes special HTML entities back to the corresponding characters. yii\helpers\BaseHtml dropDownList() Generates a drop-down list. yii\helpers\BaseHtml encode() Encodes special characters into HTML entities. yii\helpers\BaseHtml endForm() Generates a form end tag. yii\helpers\BaseHtml endTag() Generates an end tag. yii\helpers\BaseHtml error() Generates a tag that contains the first validation error of the specified model attribute. yii\helpers\BaseHtml errorSummary() Generates a summary of the validation errors. yii\helpers\BaseHtml escapeJsRegularExpression() Escapes regular expression to use in JavaScript. yii\helpers\BaseHtml fileInput() Generates a file input field. yii\helpers\BaseHtml getAttributeName() Returns the real attribute name from the given attribute expression. yii\helpers\BaseHtml getAttributeValue() Returns the value of the specified attribute name or expression. yii\helpers\BaseHtml getInputId() Generates an appropriate input ID for the specified attribute name or expression. yii\helpers\BaseHtml getInputIdByName() Converts input name to ID. yii\helpers\BaseHtml getInputName() Generates an appropriate input name for the specified attribute name or expression. yii\helpers\BaseHtml hiddenInput() Generates a hidden input field. yii\helpers\BaseHtml img() Generates an image tag. yii\helpers\BaseHtml input() Generates an input type of the given type. yii\helpers\BaseHtml jsFile() Generates a script tag that refers to an external JavaScript file. yii\helpers\BaseHtml label() Generates a label tag. yii\helpers\BaseHtml listBox() Generates a list box. yii\helpers\BaseHtml mailto() Generates a mailto hyperlink. yii\helpers\BaseHtml ol() Generates an ordered list. yii\helpers\BaseHtml passwordInput() Generates a password input field. yii\helpers\BaseHtml radio() Generates a radio button input. yii\helpers\BaseHtml radioList() Generates a list of radio buttons. yii\helpers\BaseHtml removeCssClass() Removes a CSS class from the specified options. yii\helpers\BaseHtml removeCssStyle() Removes the specified CSS style from the HTML options. yii\helpers\BaseHtml renderSelectOptions() Renders the option tags that can be used by dropDownList() and listBox(). yii\helpers\BaseHtml renderTagAttributes() Renders the HTML tag attributes. yii\helpers\BaseHtml resetButton() Generates a reset button tag. yii\helpers\BaseHtml resetInput() Generates a reset input button. yii\helpers\BaseHtml script() Generates a script tag. yii\helpers\BaseHtml style() Generates a style tag. yii\helpers\BaseHtml submitButton() Generates a submit button tag. yii\helpers\BaseHtml submitInput() Generates a submit input button. yii\helpers\BaseHtml tag() Generates a complete HTML tag. yii\helpers\BaseHtml textInput() Generates a text input field. yii\helpers\BaseHtml textarea() Generates a text area input. yii\helpers\BaseHtml ul() Generates an unordered list. yii\helpers\BaseHtml
Protected Methods
Method Description Defined By activeBooleanInput() Generates a boolean input This method is mainly called by activeCheckbox() and activeRadio(). yii\helpers\BaseHtml activeListInput() Generates a list of input fields. yii\helpers\BaseHtml booleanInput() Generates a boolean input. yii\helpers\BaseHtml setActivePlaceholder() Generate placeholder from model attribute label. yii\helpers\BaseHtml