Change the font color - html

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

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 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 - set label position for input field

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

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.