I have a _form.php file with this field:
<?=
$form->field($model, 'price')
->textInput([
'class' => 'form-control',
'type' => 'number'
])
?>
The price has this format 1234.50. I would like to have the format es-AR, like this: 1234,50.
In the GridView of index.php I use this code and it works great so I would like to do the same in the _form but it is not working.
[
'attribute' => 'price',
'value' => function($myModel) {
$myFormat = new NumberFormatter("es-AR", NumberFormatter::CURRENCY);
return $myFormat->formatCurrency($myModel->price, "ARS");
},
]
There are 2 ways to do that:
Add extra class to the price field and use javascript to convert to format you want (remember to return it back on submit)
Create priceFormat() and use it on AfterFind event and remember to use priceUnFormat() to return to decimal on BeforeSave
Use:
$form->field($model, 'attr', ['inputOptions' => ['value' => Yii::$app->formatter->asDecimal($model->attr)]])
i am using a dropdown list and i wants to know is there any way i can get the value selected by user instead of id.
I am using yii2 activeform with ArrayHelper map to populate the options which is like below
<?= $form->field($model, 'carname')->dropDownList(
ArrayHelper::map(Cars::find()->all(), 'id', 'name'),
['prompt' => 'Select Car Name']
) ?>
In controller on form submit it returns id which is good but i want to save the name directly in the database.
Thank you
<?= $form->field($model, 'carname')->dropDownList(
ArrayHelper::map(Cars::find()->all(), 'name', 'name'),
['prompt' => 'Select Car Name']
) ?>
Yii is the best but I'm have two models , book and author . I need to process two forms for each one in a single view , as I do this in yii2.0 .
<?=
$this->render('_formBook', array('model' => $model));
?>
<?=
$this->render('_formAuthor', array('model' => $model));
?>
try this way
In your Controller
$modelBook = Book::findOne($id);
$modelAuthor = Author::findOne($key);
return $this->render('_formWithTheTwoForm',
'modelBook' => $modelBook,
'modelAuthor' => $modelAuthor,]);
Then in your view you can use both the model
I want to show selected value in Yii2 dropdown,
$_GET Value:
$id = $_GET["cid"];
Drop down code
$form->field($model, 'userid')
->dropDownList(
[User::getUser()],
//[ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name')],
['prompt'=>'Select a user','id'=>'user_dropdown'],
['options' =>
[
$id => ['selected' => true]
]
]
)->label('');
but this method is not working!
Try this.
$model->userid=$id;
$form->field($model, 'userid')
->dropDownList(...)
->label('');
Basically, you affect the options (your <option> elements) by using the value attribute's actual value as the array key in the dropDownList options array.
So in this case I have an array of states and the value attributes have the state abbreviation, for example value="FL". I'm getting my selected state from the Address table, which stores the abbreviation, so all I have to do is use that as my array key in the options array:
echo $form->field($model, 'state')->dropDownList($listData, ['prompt'=>'Select...', 'options'=>[$address->state=>["Selected"=>true]]]);
The documentation spells it out: http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#dropDownList()-detail
i hope this will help you
$form->field($model, 'userid')
->dropDownList(
[User::getUser()],
//[ArrayHelper::map(User::find()->where('id' => $id)->all(), 'id', 'name')],
['prompt'=>'Select a user','id'=>'user_dropdown'],
['options' =>
[
$id => ['selected' => true]
]
]
)->label('');
$model->userid = $_GET['cid'];
$form->field($model, 'userid')
->dropDownList(
$items, //Flat array('id'=>'val')
['prompt'=>''] //options
)->label('');
<?php
$selectValue = $_GET['tid']
echo $form->field($model, 'tag_id')
->dropdownList(
ArrayHelper::map(Tag::find()->where(['visibility'=>'1'])->orderBy('value ASC')->all(), 'tag_id', 'value'),
['options' => [$selectValue => ['Selected'=>'selected']]],
['prompt' => '-- Select Tag --'])
->label(false);
?>
This code will Auto Select the selected value received as input.
Where $selectValue will be numeric value received from GET method.
Final output : <option value="14" selected="selected">NONE</option>
Ok, if you are using ActiveForm then value of your model field will be used as the selected value. With Html helper dropDownList function accepts another parameter selection doc. Example:
$id = $_GET["cid"];
\yii\helpers\Html::dropDownList('userid', $id, [ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name'), [......])
This is my S.O.L.I.D approach.
Controller
$model = new User();
$model->userid = $id; #this line does the magick. Make sure the $id has a value, so do the if else here.
return $this->return('view', compact('model'))
But, if you prefer the setter method. Do this...
# Model
class User extends ActiveRecord
{
public function setUserId(int $userId): void
{
$this->userid = $userId;
}
}
# Controller
$model = new User();
$model->setUserId($userId);
View (view is as-is)
$form->field($model, 'userid')
->dropDownList(...)
->label('');
Use this code below:
$category = \backend\models\ProductCategory::find()->WHERE(['deleted'=>'N'])->all();
$listData = ArrayHelper::map($category,'product_category_id','category_name');
echo $form->field($model, 'product_category_id')->dropDownList($listData,['prompt'=>'Select']);
All of the options I've added are unrequired.
What is written in the 'value' index is what dropdown item will be selected as default.
Prompt just displays a first option that doesn't have a value associated with it.
echo $form->field($model, 'model_attribute_name')
->dropDownList($associativeArrayValueToText,
[
'value'=> $valueIWantSelected,
'prompt' => 'What I want as a placeholder for first option',
'class' => 'classname'
]);
You'll find the function that assigns this in the following file:
vendor/yiisoft/yii2/helpers/BaseHtml.php
public static function renderSelectOptions($selection, $items, &$tagOptions = [])
Also from the function you can see that you can add an optgroup to your dropdown, you just need to supply a multidimensional array in where I've put $associativeArrayValueToText. This just means that you can split your options by introducing group headings to the dropdown.
This cakephp form used to edit a mysql record needs to load the state of a radio button from a mysql database.
The mysql payment_type is enum('Account', 'Credit').
All of the other non-radio-button form inputs reload from the database and payment_type is correctly displayed on another form using this:
<?php echo h($purchaseOrder['PurchaseOrder']['payment_type']); ?>
Why doesn't this correctly set the radio-button from payment_type?
$options = array('account' => 'Account', 'credit' => 'Credit');
$attributes = array('legend' => false, 'value' => 'payment_type');
echo $this->Form->radio('payment_type', $options, $attributes);
In your attribute array, you should assign value which you want to keep selected by default .
For example you want account to be selected by default then in value you should assign 'account'. So your final attribute will be:
$attributes = array('legend' => false, 'value' => 'account');