wilsonim
(Wilson Im)
July 14, 2016, 3:24am
1
Hi All, I’m newbie.
I try to make dropdown menu like this
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
[
'label' => 'User Options',
'icon' => 'menu-down',
'items' => [
[
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/site/login']]
) : (
'<li>'
. Html::beginForm(['/site/logout'], 'post', ['class' => 'navbar-form'])
. Html::submitButton(
'Logout (' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link']
)
. Html::endForm()
. '</li>'
)
],
[
'label' => 'Change Password',
'icon' => 'lock',
'url' => yii\helpers\Url::to(['/user/change', 'id' => Yii::$app->user->identity])
],
]
]
],
]);
NavBar::end();
But i got error like this below
Invalid Configuration – yii\base\InvalidConfigException
The ‘label’ option is required.
Please help.
Thanks before.
softark
(Softark)
July 14, 2016, 10:46pm
2
Try this:
'items' => [
[
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/site/login']]
) : (
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']]
)
],
wilsonim
(Wilson Im)
July 15, 2016, 5:11am
3
softark:
Try this:
'items' => [
[
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/site/login']]
) : (
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']]
)
],
thanks for your help. It works.
wilsonim
(Wilson Im)
July 15, 2016, 5:16am
4
I have one more question
[
'label' => 'Change Password',
'url' => yii\helpers\Url::to(['/user/reset', 'id' => Yii::$app->user->isGuest ? '' : Yii::$app->user->identity->id]),
'visible' => !Yii::$app->user->isGuest
] ]
when i click the url, it goes to url
http://localhost/pks/web/index.php?r=user%2Freset.
It supposed to be
http://localhost/pks/web/index.php?r=user%2Freset&id=1
I don’t know why.
Thanks before.
softark
(Softark)
July 15, 2016, 6:04am
5
It looks strange. I also don’t understand why.
Try something like this for debugging:
[
'label' => 'Change Password',
'url' => yii\helpers\Url::to(['/user/reset', 'id' => Yii::$app->user->isGuest ? '' : Yii::$app->user->identity->id]),
'visible' => !Yii::$app->user->isGuest
],
[
'label' => yii\helpers\Url::to(['/user/reset', 'id' => Yii::$app->user->isGuest ? '' : Yii::$app->user->identity->id]),
],
wilsonim
(Wilson Im)
July 15, 2016, 7:17am
6
Thanks for your reply.
I already found my mistake.
I create action in user controller
public function actionReset($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()))
{
$model->setPassword($model->password);
$model->generateAuthKey();
$model->updated_at = date('Y-m-d h:i:s');
$model->save();
return $this->redirect(['reset', 'id' => $model->id]);
}
else {
echo 'not loaded';
//return $this->redirect(['reset', 'model' => $model]);
}
}
and I render it in view reset.php, and the link i made in navbar
[
'label' => 'Change Password',
'url' => yii\helpers\Url::to(['/user/reset', 'id' => Yii::$app->user->isGuest ? '' : Yii::$app->user->identity->id]),
'visible' => !Yii::$app->user->isGuest
]
It always go to not loaded. why?
softark
(Softark)
July 15, 2016, 7:45am
7
The link in the nav bar is working as expected. It will request user/reset with the user’s id, but without post data. The post data have to be sent by user/reset itself in the next round.
public function actionReset($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()))
{
// You'll come here when you post data from password resetting form.
$model->setPassword($model->password);
$model->generateAuthKey();
$model->updated_at = date('Y-m-d h:i:s');
$model->save();
// return $this->redirect(['reset', 'id' => $model->id]);
// You must redirect to somewhere else.
return $this->redirect(['index']);
}
else {
// When you first call user/reset, you'll come here.
// Now, you display the form for password resetting.
return $this->render('reset', ['model' => $model]);
}
}
Note that view file has no effect without being rendered in the controller.
wilsonim
(Wilson Im)
July 15, 2016, 8:53am
8
Thanks a lot for your detail explanation. I really appreciate.
There is a newbie question. If I have a model user with required fields let’s say username, password, and email.
In this case (reset password), i want to render only password field.
Which is the best way to load the model without loading the other field?
Thanks before.
softark
(Softark)
July 15, 2016, 12:37pm
9
$model = $this->findModel($id);
This will load all the fields of User model.
But:
$model->load(Yii::$app->request->post())
this will change only the fields that are in the post data sent from the form. Other fields remain unchanged, and will pass the validation.
So you can safely exclude “username”, “email”, or any other required field from your form if you don’t need to modify it.
wilsonim
(Wilson Im)
July 16, 2016, 8:38am
10
I really thanks to you about these important knowledges.
Thanks.