yob
(Yamiloro)
April 18, 2015, 3:55am
#1
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
yob
(Yamiloro)
April 18, 2015, 3:57am
#2
the error:
PHP Notice – yii\base\ErrorException
Undefined variable: nivel_nombre
it cant continue rendering
flarpy
(Peter)
April 18, 2015, 8:58am
#3
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.
yob
(Yamiloro)
April 18, 2015, 2:02pm
#4
it’s strange…but I did that…in the code of my controllet I pass the variable as parameter…
I dont know what happend…
Griffolion
(Andyhall1989)
April 19, 2015, 1:06am
#5
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.
Bizley
(Bizley)
April 19, 2015, 7:47am
#7
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 */
]) ?>
yob
(Yamiloro)
April 20, 2015, 3:42am
#9
hello guys, thanks for your help.
I used $_POST and it worked.
thanks and have a great week.
yob
(Yamiloro)
April 22, 2015, 1:38am
#10
you are right andix, for curiosity I added the code that you recommended me…in the create view
<?= $this->render(’_form’, [
'model' => $model,
'nivel_nombre' => $nivel_nombre, /* add this */
]) ?>
thanks guys…Its better that way I think, its like the framework works.