bambinou
(Benoitrivaux)
1
Hello,
I am trying to pass the logged in user identity in a _form field but cannot work out how to do it, could you please help me?
//This is what I tried but get errors
<?= $form->field($model, $user = \Yii::$app->user->identity); ?>
Thanks,
Ben
If user identity has a username field, that will be:
//This is what I tried but get errors
<?= $form->field($model, $user = \Yii::$app->user->identity->username); ?>
otherwise you are passing entire object.
bambinou
(Benoitrivaux)
3
Sorry Fabrizio I did not understand what you mean.
With the above code I get this:
PHP User Error – yii\base\ErrorException
Exception 'yii\base\InvalidParamException' with message 'Attribute name must contain word characters only.'
in C:\xampp\htdocs\forum\vendor\yiisoft\yii2\helpers\BaseHtml.php:1933
Stack trace:
#0 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\helpers\BaseHtml.php(1153): yii\helpers\BaseHtml::getInputName(Object(frontend\models\Posts), NULL)
#1 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\helpers\BaseHtml.php(1191): yii\helpers\BaseHtml::activeInput('text', Object(frontend\models\Posts), NULL, Array)
#2 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\widgets\ActiveField.php(184): yii\helpers\BaseHtml::activeTextInput(Object(frontend\models\Posts), NULL, Array)
#3 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\widgets\ActiveField.php(156): yii\widgets\ActiveField->render()
#4 C:\xampp\htdocs\forum\frontend\views\posts\_form.php(31): yii\widgets\ActiveField->__toString()
#5 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\View.php(325): require('C:\\xampp\\htdocs...')
#6 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\View.php(247): yii\base\View->renderPhpFile('C:\\xampp\\htdocs...', Array)
#7 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\View.php(149): yii\base\View->renderFile('C:\\xampp\\htdocs...', Array, NULL)
#8 C:\xampp\htdocs\forum\frontend\views\posts\create.php(19): yii\base\View->render('_form', Array)
#9 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\View.php(325): require('C:\\xampp\\htdocs...')
#10 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\View.php(247): yii\base\View->renderPhpFile('C:\\xampp\\htdocs...', Array)
#11 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\View.php(149): yii\base\View->renderFile('C:\\xampp\\htdocs...', Array, Object(frontend\controllers\PostsController))
#12 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\Controller.php(367): yii\base\View->render('create', Array, Object(frontend\controllers\PostsController))
#13 C:\xampp\htdocs\forum\frontend\controllers\PostsController.php(70): yii\base\Controller->render('create', Array)
#14 [internal function]: frontend\controllers\PostsController->actionCreate()
#15 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)
#16 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\Controller.php(151): yii\base\InlineAction->runWithParams(Array)
#17 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('create', Array)
#18 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\web\Application.php(83): yii\base\Module->runAction('posts/create', Array)
#19 C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#20 C:\xampp\htdocs\forum\frontend\web\index.php(18): yii\base\Application->run()
#21 {main}
Thank you.
Ben
What Fabrizio wanted to tell you is that the user identity holds attributes of the currently logged in user…
Try for example:
echo \Yii::$app->user->identity->username; // gives out the username
echo \Yii::$app->user->identity->id; // gives the ID
Regarding your question it should be something like:
echo $form->field($model, 'yourFieldName')->textInput(['value' => \Yii::$app->user->identity->id;]);
"yourFieldName" must be the name of the attribute you want to fill.
For example "user_id" or whatever you called it in your DB.
Sorry, I thought that I was writing in textInput method …
MetaCrawler answered to you.
bambinou
(Benoitrivaux)
6
Thanks to both of you.
Got it working, I have an error about object not set but this was because I was not logged in :-))
Thanks again!
Ben