how to pass values from database to kartik checkbox list - yii2

i have passed some values($list) fetched from database to yii2 default checkbox list and it is successfully showing the result
<?php $list=ArrayHelper::map(Questions::find()->all(),'id','question'); ?>
<?= $form->field($model, 'dept_id')->checkboxList($list); ?>
how can i pass the same $list with kartik checkboxlist,shown below
<?= $form->field($model, 'dept_id')->widget(CheckboxX::classname($list), [
'initInputType' => CheckboxX::INPUT_CHECKBOX,
'autoLabel' => true
])->label(false); ?>

This widget allows three checkbox states [1, 0 and null]
You could use Yii2: ActiveField CheckboxList Instead. This way you can specify *n options to in form of checkboxes.

Related

How to display a HTML tag in Yii2 form error summary

I am trying to display a link in error message in login for, but it is not working.
The error message in LoginForm valdiation:
$this->addError($attribute, 'Your account has been disabled. Enable It');
In login.php (view):
<?= $form->errorSummary($model); ?>
I tried like below, but not working:
<?= $form->errorSummary($model,['errorOptions' => ['encode' => false,'class' => 'help-block']]); ?>
I am getting the following output instead of rendered a tag:
You need to disable encoding at ActiveForm level using encodeErrorSummary property, if you want to use $form->errorSummary($model):
<?= $form = ActiveForm::begin([
'id' => 'login-form',
'encodeErrorSummary' => false,
'errorSummaryCssClass' => 'help-block',
]) ?>
<?= $form->errorSummary($model) ?>
Alternatively you may use Html::errorSummary() directly:
<?= Html::errorSummary($model, ['encode' => false]) ?>

How to user MaksedInput definitions property in Yii2

I need to understand how to use MaskedInput definitions property because i have "phone" field and it have always contain + at start and after it it have contain 10-15 decimal characters. How to implement it?
I've used something like this
<?= $form->field($model, 'phone')->widget(\yii\widgets\MaskedInput::className(), [
'mask' => '99-99999999-9',
]) ?>
See http://www.yiiframework.com/doc-2.0/yii-widgets-maskedinput.html and https://github.com/RobinHerbots/Inputmask
You could try
<?= $form->field($model, 'phone')->widget(\yii\widgets\MaskedInput::className(), [
'alias' => 'phone',
]) ?>

Yii2-user, dektrium-yii2-user, Yii2 Populate a dropdown in yii2

I am pretty sure that there is a better way to populate the array, needed for the dropdown:
<?php
$items2 = [Yii::$app->user->identity->id => Yii::$app->user->identity->username ]; ?>
<!--...some html -->
<?= $form->field($model, 'idUser')->dropDownList($items2,['Item' => ''])?>
already try:
$item2 = ArrayHelper::map(Yii::$app->user->identity::find()->all(), 'id', 'name');
reason, I want to display 'name' but submit 'value'='id'.
Should be this
<?= $form->field($model, 'idUser')->
dropDownList(ArrayHelper::map(Yii::$app->user->identity->find()->all(),
'id', 'username'), ['prompt'=>'Select...'])?>

Yii2 Add a Form field not in model

As we know,
<?= $form->field($model, 'name_field')->textInput() ?>
Adds a text field connected to 'name_field' in the model/table.
I want to add a field NOT in the model/table, and then run some JS when it loses focus to calculate the other fields.
How firstly do you add a free text field not connected to the model ?
Second, does anyone have any examples of adding JS/Jquery to the _form.php ?
The Html class contains the functions for generation of fields. In fact, your code above ends up calling Html::textInput(). To add a field
<?= Html::textInput("name", $value) ?>
To add javascript to a view just use registerJs():
$this->registerJs("alert('true');");
You can have the field rendered the same way as the ActiveField, with a label and classes. For example, let’s add a Cc field to a Mail form.
First display the To: field (in the model):
<?= $form->field($model, 'to')->textInput() ?>
Let’s add the Cc field (not in the model):
<?= Html::beginTag('div', ['class' => 'form-group field-mail-cc']) ?>
<?= Html::label('Cc:', 'mail-cc', ['class' => 'control-label']) ?>
<?= Html::textInput('Mail[cc]', '', ['id' => 'mail-cc', 'class' => 'form-control']) ?>
<?= Html::endTag('div') ?>
The class and id names mail-cc and field-mail-cc follow the ActiveForm naming pattern. The input name Mail[cc] adds your field to the ActiveForm group, so you can easily retrieve it with the usual
$form = Yii::$app->request->post('Mail');

DropDownList yii 2.0 example

I am using yii 2.0 Framework.
How i can make options from my database.
I found this, but it's yii 1.1:
<?php echo CHtml::dropDownList('listname', $select,
array('M' => 'Male', 'F' => 'Female'));
I want to pass it to form:
<?php $form->dropDownList() ?>
How i can fill my dropdownlist from my database table?
If you use ActiveForm widget use this:
<?php
$items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
$form->field($model, 'attribute')->dropDownList($items)
?>
Use yii\helpers\Html it contains Html::dropDownList().
echo Html::dropDownList('listname', $select, ['M'=>'Male', 'F'=>'Female']);
Check Yii Framework 2.0 API
Controller
public function actionSomething() {
$sexes = ['M'=>'Male', 'F'=>'Female'];
$this->render('yourView', ['sexes'=>$sexes]);
}
View
<?php
::
echo Html::dropDownList('listname', $select, $sexes);
::
?>
Yes if you use ActiveForm widget , u dont have to change anything in the controller , in views, in the form, add this where u want the dropdown
use yii\helpers\ArrayHelper;
<?php
$city = \app\models\City::find()->all();
$listData=ArrayHelper::map($city,'cityId','cityName');
?>
<?= $form->field($model, 'cityId')->dropDownList($listData,['prompt'=>'Choose...']) ?>
<?= $form->field($model, 'name_of_field')->dropdownList(['1' => 'aaa', '2' => 'bbb'], ['prompt' => '---Select Data---']) ?>