0

I am new to Yii2 Framework and I need to understand the users status. I need to create an app that assign many statuses to the user, comparing to just 2 that Yii2 gave. In Yii2 common\models\Users there are two constants: STATUS_DELETED = 0; and STATUS_ACTIVE = 10;. There is a block of code that limits the range of the value of status to 0-10, which is STATUS_DELETED and STATUS_ACTIVE. If I need to add other status like STATUS_DISABLED = 20 in the rules() part, how do I modify this block? Do I need to remove the STATUS_ACTIVEand replace it with STATUS_DISABLED, or I should just limit the value of STATUS_DISABLED to be within the range of 0-10. How do I make this work? I don't understand this part.

class User extends ActiveRecord implements IdentityInterface
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const STATUS_DISABLED = 20; // I want to add this


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
        ];
    }

}
rob006
  • 21,383
  • 5
  • 53
  • 74
sam
  • 853
  • 3
  • 19
  • 50
  • 1
    Did you tried to add this new status to rules: `['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_DISABLED]]`? From your sample this is the only thing that could disallow this status. – rob006 Jun 16 '18 at 08:29
  • @ rob006 my problem is am not sure if i'm suppose to add the new status within the rules since the rule specify a range and usually range is between two numbers. example 0 t0 10, 50 to 100 etc, so adding the new status will make it like range between 0, 10 and 20 which look so inappropriate and incorrect to me, than that's while am asking this question – sam Jun 16 '18 at 08:45
  • 1
    This is not range in the meaning "between A and B". This actually works as "A or B or C or ..." so you may way use any number of values and value of attribute should match one of them to pass the rule. – rob006 Jun 16 '18 at 08:48
  • @ rob006 now i understand, thanks so muck for your help – sam Jun 16 '18 at 08:49
  • 1
    @rob006 add it as an answer, will help others in future – Muhammad Omer Aslam Jun 18 '18 at 23:27

1 Answers1

3

$range should contain array of valid values, not actual range in the meaning of "between A and B". So this:

['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],

means that status should be either 0 or 10. For example 5 will not be valid value. And you can add any number of values to range array, like this:

[
    'status', 'in', 
    'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_DISABLED]
],
rob006
  • 21,383
  • 5
  • 53
  • 74