3

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?

prattom
  • 1,625
  • 11
  • 42
  • 67

2 Answers2

3

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 :)

Joey Estrada
  • 404
  • 2
  • 6
  • 10
0
        <div class="row">
            <?php echo $form->labelEx($user,'accountID'); ?>
            <?php echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32, 'disabled'=>Yii::app()->user->checkAccess('admin'))); ?>
            <?php echo $form->error($user,'accountID'); ?>
        </div>
Daniel Vaquero
  • 1,315
  • 1
  • 8
  • 13