Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Disable a button in Yii2

Tags:

php

button

yii2

I'm trying to disable the Create Project Button when the user is not logged in, the button will Hide or disable.

And this is my condition:

<p>
    <?php
    if (Yii::$app->user->isGuest) {
        Html::a('Create a Project', ['create'], ['class' => 'btn btn-primary btn-xs']);
    } elseif(Yii::$app->user->identity->username) {
        Html::a('Create a Project', ['create'], ['class' => 'btn btn-success']);
    }
    ?>
</p>

It's working, But, when the user is logged in, the button is already Hide!

How can disable or hide the button in Yii2 and fix that problem?

is there any tutorial about that?

like image 345
mandev Avatar asked Jan 28 '26 14:01

mandev


1 Answers

You need to add a disabled attribute to disable the button, or to hide it completely you can use CSS style=display: none;

Both are used in the code below

<p>
    <?php
        if (Yii::$app->user->isGuest) {
            // This button will be displayed, but is disabled 
            Html::a('Create a Project', ['create'], ['class' => 'btn btn-primary btn-xs', 'disabled' => 'disabled']);
        } elseif(Yii::$app->user->identity->username) {
            Html::a('Create a Project', ['create'], ['class' => 'btn btn-success']);
        } else {
            // This button will not be displayed (it is hidden)
            Html::a('Create a Project', ['create'], ['class' => 'btn btn-primary btn-xs', 'style' => 'display: none;']);
        }
    ?>
</p>
like image 144
Lynch Avatar answered Jan 31 '26 03:01

Lynch