Skip to content

Commit 7eff133

Browse files
committed
Avoid validating many-to-many relation by default
1 parent 2dc3892 commit 7eff133

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Yii LinkMany Change Log
22
=======================
33

4+
1.1.4 Under development
5+
-----------------------
6+
7+
- Avoid validating many-to-many relationships by default
8+
49
1.1.3 Sep 14, 2022
510
------------------
611

src/LinkManyBehavior.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ public function afterValidate()
119119
foreach ($this->relations as $definition) {
120120
$relationName = $definition->name;
121121

122-
if (!$definition->validate) {
122+
$relation = $this->owner->getRelation($relationName);
123+
124+
$validate = $definition->validate === null ? !$relation->via : $definition->validate;
125+
126+
if (!$validate) {
123127
continue;
124128
}
125129

@@ -131,7 +135,7 @@ public function afterValidate()
131135
}
132136

133137
foreach ($models as $i => $model) {
134-
if (!$model->validate()) {
138+
if (!$relation->via && !$model->validate()) {
135139
$attribute = $relationName . "[$i]";
136140
$this->addError($model, $attribute);
137141
}

src/RelationDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class RelationDefinition extends BaseObject
1919
/**
2020
* @var string whether the relation validation is required.
2121
*/
22-
public $validate = true;
22+
public $validate = null;
2323

2424
/**
2525
* @var boolean whether to delete the pivot model or table row on unlink.

0 commit comments

Comments
 (0)