Skip to content

Commit 076c728

Browse files
committed
Show validation index error
1 parent d666ed3 commit 076c728

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
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.0.2 Under development
5+
-----------------------
6+
7+
- Show validation index error
8+
49
1.0.1 Jan 17, 2022
510
------------------
611

src/LinkManyBehavior.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,25 @@ public function afterSave()
117117
public function afterValidate()
118118
{
119119
foreach ($this->relations as $definition) {
120-
$name = $definition->name;
120+
$relationName = $definition->name;
121121

122122
if (!$definition->validate) {
123123
continue;
124124
}
125125

126-
$errors = [];
127-
$models = ArrayHelper::getValue($this->_inserteds, $name, []);
128-
$updateds = ArrayHelper::getValue($this->_updateds, $name, []);
126+
$models = ArrayHelper::getValue($this->_inserteds, $relationName, []);
127+
$updateds = ArrayHelper::getValue($this->_updateds, $relationName, []);
129128

130129
foreach($updateds as $model) {
131130
$models[] = $model;
132131
}
133132

134-
foreach ($models as $model) {
133+
foreach ($models as $i => $model) {
135134
if (!$model->validate()) {
136-
$errors[] = $model->getErrors();
135+
$attribute = $relationName . "[$i]";
136+
$this->addError($model, $attribute);
137137
}
138138
}
139-
140-
foreach ($errors as $error) {
141-
$this->owner->addError($name, $error);
142-
}
143139
}
144140
}
145141

@@ -387,4 +383,19 @@ private function findDefinition($name)
387383
}
388384
return null;
389385
}
386+
387+
/**
388+
* Attach errors to owner relational attributes
389+
* @param \yii\db\ActiveRecord $model
390+
* @param string $relationName
391+
* @return void
392+
*/
393+
private function addError($model, $relationName)
394+
{
395+
foreach ($model->getErrors() as $attributeErrors) {
396+
foreach ($attributeErrors as $error) {
397+
$this->owner->addError($relationName, $error);
398+
}
399+
}
400+
}
390401
}

tests/LinkManyBehaviorTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,35 @@ public function testValidate()
230230
$this->assertCount(2, $errors['comments']);
231231
}
232232

233+
public function testValidateErrorIndex()
234+
{
235+
$post = Post::findOne(1);
236+
$post->fill([
237+
'comments' => [
238+
[
239+
'subject' => 'Valid subject',
240+
'content' => 'Valid content',
241+
],[
242+
'subject' => 'Valid subject',
243+
'content' => 6789
244+
],[
245+
'subject' => 1234
246+
],
247+
]
248+
],'');
249+
250+
$post->validate();
251+
252+
$this->assertEquals([
253+
'comments[1]' => [
254+
'Content must be a string.'
255+
],
256+
'comments[2]' => [
257+
'Subject must be a string.',
258+
'Content cannot be blank.'
259+
]
260+
], $post->getErrors());
261+
}
233262

234263
public function testSetRelation()
235264
{

0 commit comments

Comments
 (0)