25

I can define a rule for a single integer like this:

[['x'], 'integer']

Is it possible to tell that x is an integer array? For example:

[['x'], 'integer[]']

And could I specify the valid values in the array?

Update: From Yii version 2.0.4 we've got some help. See this answer.

Community
  • 1
  • 1
robsch
  • 9,358
  • 9
  • 63
  • 104
  • Is this for model rules? You can't really store an array, maybe a serialization of it. Do you wanna try to make yourself a bit clearer? – casraf Dec 02 '14 at 15:26
  • In my case it is model for a form that contains some checkboxes that are loaded as an array (with $modal->load(Yii::$app->request->post()). So it is not for an ActiveRecord object. – robsch Dec 02 '14 at 15:49
  • @Chen Asraf You can store an array if you use **mongodb** – verybadbug Mar 04 '15 at 03:11

3 Answers3

67

From version 2.0.4 there is the new EachValidator which makes it more easy now:

['x', 'each', 'rule' => ['integer']],

This should be sufficient. If the values should be also checked you could use this (with the 'in' validator which actually is the RangeValidator):

['x', 'each', 'rule' => ['in', 'range' => [2, 4, 6, 8]]], // each value in x can only be 2, 4, 6 or 8

However, you can use this 'in' validator also directly. And that is possible with Yii versions before 2.0.4:

['x', 'in', 'range' => [2, 4, 6, 8], 'allowArray' => true]

The use of 'strict' => true would probably makes no sense in case the data is sent by the client and is set with Model->load(). I'm not quite sure but I think those values are all sent as strings (like "5" instead of 5).

robsch
  • 9,358
  • 9
  • 63
  • 104
  • ['x', 'each', 'rule' => ['in', 'range' => [2, 4, 6, 8]]] will not work if x is an array of ints (not a scalar type). It should be done like you described below ['x', 'each', 'rule' => ['in', 'allowArray' => true, 'range' => [2, 4, 6, 8]]]. I've spent some time while I defined where exactly I'm doing something wrong. Nevertheless thanks for your answer – Yehor Apr 14 '16 at 05:30
  • 1
    ['x', 'each', 'rule' => ['integer']] I am using this exact line in my SearchModel but it doesn't work... It seems to ignore the rule... Why? – webpaul Feb 03 '17 at 11:09
  • @webpaul Hard to say. Create a new question with detailed information. – robsch Feb 03 '17 at 12:28
18

You may need to create custom validation rules like below:

['x','checkIsArray']

Then in your model, impelement checkIsArray:

public function checkIsArray(){
     if(!is_array($this->x)){
         $this->addError('x','X is not array!');
     }
}

You can do all you need into a custom validation rule.


As emte mentioned on comment, you can also use inline validator with anonymous function like below:

['x',function ($attribute, $params) {
    if(!is_array($this->x)){
         $this->addError('x','X is not array!');
     }
}]
Ali MasudianPour
  • 14,329
  • 3
  • 60
  • 62
5

If you need to check against specific range for each array element

['x', 'required'] 

plus

['x', 'each', 'rule' => ['in',  'allowArray' => true, 'range' => [2, 4, 6, 8]]]

or

['x', 'in', 'allowArray' => true,  'range' => [2, 4, 6, 8] ]  
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
Yehor
  • 6,203
  • 3
  • 20
  • 28