Yii2 - set label position for input field - yii2

I using form field and set label like this :
<?= $form->field($unit, 'estimated_time')->textInput(['style' => 'width: 50px'])->label("Minutes") ?>
but label display in the left side of input-field
How can I display it in right side ?

I think better solution is
<?= $form->field($unit, 'estimated_time', [
'template' => '{input}{label}{error}{hint}',
'options' => ['class' => 'form-group form-inline'],]
->textInput([
'style' => 'width: 50px; margin-right: 10px;']); ?>

Try this :
<?= $form->field($unit, 'estimated_time')->textInput(['style' => 'width: 50px'])->label('Your Label',['class'=>'label-class']) ?>
Add css as
.label-class{
float: right;
}

Assign style for your input like below:
<?= $form->field($unit, 'estimated_time', [
'template' => '<div style="float:right;">{label}</div>{input}{error}{hint}'
]);?>
more reference
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activefield.html

Related

Change the font color

In this line of code:
<b><?= Html::a(Yii::t('app', 'Download'), ['download-file', 'id' => 'SSF']) ?></b>
I want to change the font color of Download to red by putting style inside this code. How do I achieve that?
Read the docs
<?= Html::a(Yii::t('app', 'Download'), ['download-file', 'id' => 'SSF'], ['class' => 'myclass', 'style' => 'color: red;']) ?>

Cakephp3: How to output checkboxes into 2 colums?

I have this call to output a set of checkboxes within my view:
<?=$this->Form->input('roles._ids', [
'options' => $roles,
'label' => false,
'multiple' => 'checkbox',
'templates' => [
'checkboxWrapper' => '<label class="mt-checkbox">{{label}}<span></span></label>',
'nestingLabel' => '{{input}}{{text}}',
'inputContainer' => '<div class="col-md-1" style="padding-top: 10px;"><div class="mt-checkbox-list" data-error-container="#form_2_services_error">{{content}}</div></div>'
]]);
?>
Does anyone know a solution - how to break the output into 2 divs?
I wanted to have half the checkboxes in a single
<div class="col-md-1" style="padding-top: 10px;">
(see line "inputContainer") div container. Is that anyhow possible?
If you want to separate checkbox in two columns you can do like this
<?php
$this->Form->templates([
'checkboxWrapper' => '<div class="col-md-6">{{label}}</div>'
]);
?>
<?=$this->Form->input('roles._ids', [
'options' => ['asdasd','asdasd','asdasd'],
'label' => false,
'multiple' => 'checkbox',
]);
?>
OR you can just change the style of default template of cakehphp by adding this css
.checkbox {
width: 49%;
display: inline-block;
}
Hope this will help

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 ActiveForm numeric textfield

I've created an ActiveForm using yii2 like this:
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
// ** i want numeric value **
])->label(false)?>
and it rendered a result of:
<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label><input type="text" id="item-finalprice" class="form-control" name="Item[finalPrice]"><p class="help-block help-block-error"></p></span>
now i want to make it < input type="number" .. and not text.. (so user could change value using browser up/down buttons). is there any way to do it?
You can use ->textInput(['type' => 'number'] eg :
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
'type' => 'number'
])->label(false)?>
Try this . it worked for me
<?= $form->field($model, 'amount')->textInput(['type' => 'number']) ?>
Field like Phone Number/Membership No etc, some time we allow user only to enter numeric input in a text field. In such case applying pattern match rule work great for me.
Simply set a rule in the model class and you are done.
public function rules()
{
return [
...
[['contactno'], 'string', 'max' => 25],
[['contactno'], 'match' ,'pattern'=>'/^[0-9]+$/u', 'message'=> 'Contact No can Contain only numeric characters.'],
...
];
}
<?= $form->field($model, 'code')->textInput(['type'=>'number']) ?>

Getting label with colon for model's field in a form

I need to have Body: (with colon at the end), not Body rendered as label for each field in my form. How can I achieve this the best way?
I tried modifying fieldConfig => template in ActiveForm::begin by adding div class=\"\">{label}:</div> into it:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "<div class=\"\">{label}:</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
]]); ?>
but it is wrong. Colon is rendered as separate DOM element, with incorrect styling and looks ugly.
I tried doing this awfully in CSS:
.edit-label::after {
content: ":";
}
but this is even worse.
I remember, that I made a lot of stupid things in Yii1 to get this. I don't want to repeat these stupid things, when implementing this in Yii2. What is the best way of achieving this?
When using Bootstrap 3 (yii\bootstrap\ActiveField) you can use additional placeholders in the $template and you need to replace {label} with {beginLabel}{labelTitle}:{endLabel}:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => [
'class' => 'form-horizontal',
'enctype'=>'multipart/form-data'
],
'fieldConfig' => [
'template' => "<div class=\"\">{beginLabel}{labelTitle}:{endLabel}</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
],
]); ?>
I don't know, how to solve this problem, if you're using basic yii\widgets\ActiveField instead.