简体   繁体   中英

Foreign key validation rule in Yii2

I have parent_id that can have NULL value - default sets to 0 .

Model

...
[['parent_id'], 'integer'],
[['parent_id'], 'default', 'value' => 0],
[['parent_id'], 'exist', 'targetAttribute' => 'id', 'skipOnEmpty' => true],
...

But exist rule didn't work.

What I'm doing wrong?

UPDATE

Today I removed the following rule and it works:

...
[['parent_id'], 'default', 'value' => 0],
...

But what do I do if want to change the default to other value?

change the order of rules:

[['parent_id'], 'integer'],
[['parent_id'], 'exist', 'targetAttribute' => 'id', 'skipOnEmpty' => true],
[['parent_id'], 'default', 'value' => 0],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM