Yii2 Add a Form field not in model - yii2

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');

Related

Yii2 how to format <sup> tags in forms and GridView?

I have an attributeLabels for an attribute that has a <sup> tag:
public function attributeLabels() {
return [
'density' => Yii::t('yii', 'density (kg/dm<sup>3</sup>)'),
];
}
In the view it looks appropriate, but in the form and in the GridView as label it's simply like (kg/dm<sup>3</sup>)
I have tried to add labelOptions with many different format values to it, but no luck.
<?= $form->field($model, 'density', ['labelOptions' => ['format' => '(html/text/raw etc.)']])->textInput() ?>
Is it possible to make it look like a real <sup> (kg/dm3) text in the form, and if yes, can you please tell me how? Thank you.
Call it like that:
<?= $model->getAttributeLabel('density'); ?>
<?= $form->field($model, 'density')->textinput()->label(false) ?>
and after that format your template
Edit (better way):
<?= $form->field($model, 'density', ['labelOptions' => ['label' => $model->getAttributeLabel('density')]])->textinput() ?>

how to pass values from database to kartik checkbox list

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.

Cakephp 3.0 - using Chronos and datetimepicker, how to input a date without time?

In my Cakephp3.0 app, I use Chronos and datetimepicker.
In my Entity file, I have:
#property \Cake\I18n\Time $purchase_date
In my Table, I put this:
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
Then for the Validator:
$validator
->date('purchase_date', ['mdy'])
->allowEmpty('purchase_date');
In edit.ctp, I put this:
<?= $this->Form->input('purchase_date', ['empty' => true]) ?>
When I click on the glyphicon-calendar button, I keep getting a value including the time, like : "10/17/2017, 01:58 PM".
What should I do to input only Date in this format: "10/17/2017" ?
Thanks in advance.
instead of:
<?= $this->Form->input('purchase_date', ['empty' => true]) ?>
This code worked for me:
<div class='input-group date' id='datetimepicker6'><?= $this->Form->input('purchase_date', ['format' => 'mdy', 'empty' => true]) ?></div>

Yii2 Multiple input mask on a page with same ID

<?php
echo $form->field($fModel, 'cell_phone')
->widget(\yii\widgets\MaskedInput::className(),['mask' => '(999)999-9999'])
->textInput(['placeholder' => 'Phone'])->label(false);
?>
I have 2 tab with same form with one extra field on second. Issue is that it showing masked input on one tab form and not on second. Anything I am doing wrong. They have same input name/id but FORM ID is different.
You should use another id for second field both for widget and text input. Try this:
<?= $form->field($fModel, 'cell_phone')
->widget(\yii\widgets\MaskedInput::className(), ['options' => ['id' => 'another-id'], 'mask' => '(299)999-9999'])
->textInput(['id' => 'another-id', 'placeholder' => 'Phone'])->label(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',
]) ?>