1

I have a form page that one of the field is named type and it's enum('lost', 'found') and in this form, I want that field to be a dropdown list that only has these two options lost and found.
one of the suggested options was using this in view

 <?= $form->field($model, 'type')->dropDownList(
            $items,
            ['prompt'=>'']

and this in the controller

$items = ArrayHelper::map(Ads::find()->all(), 'id', 'type');  

but as you know it's just using the inserted data in the db and if I click on the dropdown list, it'll load all the lost and found options that are in the db.
Is there any way to tell yii to use the db structure and rules instead of the datas?

I have to point out that in the model I couldn't find any rules that is indicating the enum part, Is it ok? Why is it like this?
I used Gii for creating these.

public function rules()
{
    return [
        [['type', 'explanation', 'image', 'cost', 'province_id', 'address'], 'required'],
        [['type', 'explanation', 'image', 'address'], 'string'],
        [['cost'], 'integer'],
        [['province_id'], 'string', 'max' => 20],
        [['province_id'], 'exist', 'skipOnError' => true, 'targetClass' => Province::className(), 'targetAttribute' => ['province_id' => 'name']],
    ];
}  
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Amir
  • 523
  • 1
  • 5
  • 24

1 Answers1

1

When a filed type is enum and generate crud from gii, yii generate this kind of dropdown in your form automatically.

<?= $form->field($model, 'type')->dropDownList([ 'lost' => 'Lost', 'found' => 'Found', ], ['prompt' => '']) ?>

In model its type is string:

 public function rules()
    {
        return [
              [['type'], 'string'],
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130