Form Values

How do I apply a default value to a form input.


<?= $form->field($model, 'country_code') ?>

I want this to have "+61" as a default value.

https://github.com/yiisoft/yii2/blob/master/docs/guide/active-record.md#loading-default-values

or




$model = new Model;

$model->country_code = '+61';

...




Its as @zelenin mentioned…

You can set this in your controller action before you call [font="Courier New"]$model->load(Yii::$app->request->post()) [/font]




public function actionCreate() {

   $model = new YourModelClass;

   // Set all default values here

   $model->country_code = '+61';

   $model->attrib1 = 'default_value1';

   $model->attrib2 = 'default_value2';


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

       // validate and save

       if ($model->save()) {

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

       }

   }

   return $this->render('create', ['model'=>$model]);

}



Thank you both.