1

I have this scenario where I have a form as follows:

public $selling_price;
public $numbers;
public $inventory_factor;

public function rules() {
    return [
        ['selling_price'], 'integer'],
        [['inventory_factor'], 'safe'],
        ['numbers', 'each', 'rule' => ['integer']],
}

I have this last validation rule to make sure that I get an array of integers. This works fine when the input is a string for example. IT does not work though if an array [null] is sent. This for example does not throw errors

{
  "selling_price": 2200,
  "numbers": [null]
}

Using vardumper, gives the numbers array to be

[
    0 => null
] 

Is there way in Yii2 through which I can either remove(filter) the null values from the array before starting, or validating those as well?

2
  • Show us the code where you are triggering validation. Are you sure the validation is actually being executed? It is odd that you have printed here your data as an illegal JSON string with trailing comma Commented Jun 26, 2018 at 11:16
  • Yes Im sure that the validation is run completely. I just removed un-necessary parts from the json. But Ill update this, thanks for the comment :) Commented Jun 26, 2018 at 11:24

2 Answers 2

1

Having looked at the special topic for the core validators, I see that under the each validator it shows:

rule: an array specifying a validation rule. The first element in the array specifies the class name or the alias of the validator. The rest of the name-value pairs in the array are used to configure the validator object.

Also, for the yii\validators\EachValidator, which extends yii\validators\Validator it has a property $skipOnEmpty, which defaults to true:

$skipOnEmpty public property - Whether this validation rule should be skipped if the attribute value is null or an empty string.

public boolean $skipOnEmpty = true

So, accordingly, you need to tweak your rule as follows.

['numbers', 'each', 'rule' => ['integer', 'skipOnEmpty' => false]],

Now your validator for numbers will not turn a blind eye to the values in the array that are empty - if it finds any empty or non-integer values, the validation will fail.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much this is exactly what I wanted
Glad to help! Yii2's documentation is fantastic, sometimes it does take a bit of perseverance and digging for exactly what you want but 99% of the time it is all there :)
1

['numbers', 'integer', 'min' => 0]

This will Validate that the value is an integer greater than 0 if it is not empty. Normal validators have $skipOnEmpty set to true.

Reference : https://www.yiiframework.com/doc/guide/2.0/en/input-validation

in this Data Filtering topic you can refer for these

1 Comment

Hm, according to docs, 'min' specifies "the lower limit (inclusive) of the value", which I think means that 'numbers', 'integer', 'min' => 0] will validate that the value of the integer is greater than OR equal to 0? Reference: yiiframework.com/doc/guide/2.0/en/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.