Dropdown Nav

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.

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.

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.

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]),

],



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?

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.

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.




$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.

I really thanks to you about these important knowledges.

Thanks.