title: Yii2 validation rule for select field
body: <p>I have a model, for example SomeForm. There is a field. This field contains an array of selected options. Is it possible to write a rule for this field to check how many items were selected? I need to write a condition, that user has to check minimum 2 options and maximum 5. </p>
  
  <p>I tried something like this, but it doesn't work (the form can be submitted if at least one option is selected)</p>
  
  <pre><code>public function rules()
  {
      return [
          [['ingredients'], 'required'],
          ['ingredients', 'checkIsArray']
      ];
  }
  public function checkIsArray($attribute, $params)
  {
      if (empty($this-&gt;ingredients)) {
          $this-&gt;addError($attribute, "config cannot be empty");
      }
      elseif (count($this-&gt;ingredients)&gt;5) {
          $this-&gt;addError($attribute, "Choose more");
      }
      elseif (count($params)&lt;2) {
          $this-&gt;addError($attribute, "Choose less");
      }
  }
  </code></pre>