Search code examples
yiiscenarios

readonly textfield using scenarios yii


How can I disable textfield using scenarios in yii? I have 3 classes of accounts superadmin, admin and normal users. All 3 classes of users have access to update information about them but one of the field accountId can be updated by superadmin and admin only but that field should be displayed to users also. Currently I am doing it in the following way.

<div class="row">
    <?php echo $form->labelEx($user,'accountID'); ?>
    <?php
    if(Yii::app()->user->checkAccess('admin'))
        echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32)); 
    else
        echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32, 'disabled'=>'true'));?>
    <?php echo $form->error($user,'accountID'); ?>
</div>

This method has solved my problem but it is not a good method and better method is using scenarios. How can I implement same using scenarios?


Solution

  • what i do is i create a function that checks if the user has access. This will lessen my code to make it easier to maintain.

    echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32,checkAccess($userId)));?>
    
    // my function 
    function checkHTMLUserAccess($userId){
        // Some codes
        if ($hasAccess) return array('disabled'=>true);
        else return array();
    }
    

    something like that :)