Answer the question
In order to leave comments, you need to log in
Fixed values for dropdown (more than 2) - in E-NUM or where?
Hello! The following question arose:
1) there are about 10 tables (of entities) ....
2) in each of them - everything is standard (CRUD, etc.)
3) in each table there are 2-3 fields, which consist of " more than two options".. To make it clear:
- there is a table Orders ..
- each Order has a status with options: live, error, new, canceled
I want to display in the GridView in the filter those statuses that are in the values of the status
column when adding an Order and editing it, you already need to display ALL options...
While everything is done "as usual" - it is rigidly written in the views with "handles" .. but this is a "crutch"... I want to get rid of it
'filter' => Html::activeDropDownList($searchModel, 'status', ['active' => 'Active', 'live' => 'Live', 'error'=>'Error'], ['class' => 'form-control', 'prompt' => 'Все']),
[
// проверяет, что значение "level" равно Live, Active или Error
['level', 'in', 'range' => [Live, Active, Error]],
]
Answer the question
In order to leave comments, you need to log in
Good evening.
I would do it a little differently.
In the model, I would declare constants, each of which would correspond to some status.
Do not use names, only numerical values. Make the field in the table under the status smallint(6), NULL is not present, the default is 0.
Something like this:
const STATUS_LIVE = 0;
const STATUS_ERROR = 1;
/* и так далее */
public function rules()
{
return [
['status', 'integer'],
['status', 'default', 'value' => self::STATUS_LIVE],
['status', 'in', 'range' => array_keys(self::getStatusesArray())],
];
}
/* метод использовать при выводе статуса в view.php*/
public function getStatusName()
{
return ArrayHelper::getValue(self::statusesArray(), $this->status);
}
public static function getStatusesArray()
{
return [
self::STATUS_LIVE => 'Live',
self::STATUS_ERROR => 'Error',
/* и так далее */
];
}
$form->field($model, 'status')->dropDownListYOUR_MODEL_NAME::getStatusesArray(), ['prompt' => 'Select'])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question