Yii2 Use

dropdownlist examples

dropdown list with template ,use field for using template

in ur user model write getProjectOptions() function

public function getProjectOptions(){

 return[


  'abc'=>'abc,


  'xyz'=>'xyz',

}

now in user controller render this return array

   public function actionCreate()


{


    $model = new project();


    $project=$model->getProjectOptions();


        if ($model->load(Yii::$app->request->post())) {


         $model->save();


        return $this->redirect(['view', 'id' => $model->name]);


    } else {


        return $this->render('create',  // u can write _form directly or u can render to create then _form


            [


            'model' => $model,


           'project'=>$project,


          





        ]);


    }


  


   }

In view part

<?= $form->field($model, ‘user’)->dropDownList($project, [‘prompt’=>’’]); ?>

if you want to check record is already exists in database just add below rule in model rules function

[[‘attribute_name’],‘unique’],

if you want to set your own error message then use below

[['attribute_name'],'unique',message=&gt;'your message],

disable any field in update form

if u want to disable a field and restrict to user for edit the field

<?= $form->field($model, ‘abbr’)->textInput([‘readonly’ => !$model->isNewRecord]) ?>

I WILL POST DAILY YII2 USEFULL THINGS

MAKE ADVANCE APP USING MODULER BASE

if you are using gii

then u can tell gii where you can want modules in backend or frontend in advance app

first create module example country

module class will be -backend\modules\country\country

module id-country

then make model

table name-country

model class-Country(it will come itself,if table is prsent)

it will create module folder country inside modules

then in namespace

backend\modules\country\models

it will create model inside country modules inside models folder

then goto crud

model class- backend\modules\country\models\Country

search model-backend\modules\country\models\CountrySearch

controller class-backend\modules\country\controllers\CountryController

and dont forget give module id -country

then it will create crud and controller with filters

then just if you want to give any link to ur modules then use below

<a href="<?php echo Url::toRoute(’/country/country’);?>">

it will redirect to your index file of country modules