Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
583 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...