cant pass information to view :(

hi guys, I am new in yii, I have this problem, this’s my controller:

public function actionCreate()


{


    $model = new Disciplina();


    //$nivel_nombre=array();


    $nivel_nombre="hola";





    


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


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


    } else {


        return $this->render('create', array(


    'model' => $model,'nivel_nombre'=>$nivel_nombre));


        


    }


}

the view:

<?php $form = ActiveForm::begin(); ?>





<?= $form->field($model, 'nombre')->textInput(['maxlength' => 120]) ?>





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


        ArrayHelper::map(Posicion::find()->all(), 'id', 'nombre'),['prompt'=>'-Seleccione una posición-']


        )?>





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


        ArrayHelper::map(Elemento::find()->all(), 'id', 'nombre'),['prompt'=>'-Seleccione un elemento-']


        )?>

<?= $nivel_nombre;?>

thanks for your help

the error:

PHP Notice – yii\base\ErrorException

Undefined variable: nivel_nombre

it cant continue rendering

Inside your create.php file it will render the form, you need to pass $nivel_nombre from the create.php into the form as a parameter to the render options.

it’s strange…but I did that…in the code of my controllet I pass the variable as parameter…

I dont know what happend…

I’m just thinking off the cuff, but is there a chance that the ‘_’ character between nivel and nombre is messing something up? Try removing it.

Remove the underscore. You also can not camel case attributes you are passing from your controller to your view either. it’s pointless to pass a static string to your view from your controller just type it in the view.

Care to explain? You can use underscores and camel casing for the attributes passed to the view from controller as long as the variable exists and you call it in view by this very name.

@Code

Are you sure the view you are checking is the same view your controller is calling?

you might forget to pass $nivel_nombre in your create view.

Assume this ‘create view’ is default from gii,




<?= $this->render('_form', [

        'model' => $model,

    ]) ?>



that code will render _form.

the solution is




<?= $this->render('_form', [

        'model' => $model,

	'nivel_nombre' => $nivel_nombre, /* add this */

    ]) ?>



hello guys, thanks for your help.

I used $_POST and it worked.

thanks and have a great week.

you are right andix, for curiosity I added the code that you recommended me…in the create view

<?= $this->render(’_form’, [

    'model' =&gt; &#036;model,


    'nivel_nombre' =&gt; &#036;nivel_nombre, /* add this */


]) ?&gt;

thanks guys…Its better that way I think, its like the framework works.