Browse Source

#14151: Added attribute name to AttributesBehavior callback signature

See 6ffa5fb087 (commitcomment-23630375)
tags/2.0.13
Alexander Makarov 7 years ago
parent
commit
e9fad56381
No known key found for this signature in database
GPG Key ID: 3617B79C6A325E4A
  1. 6
      framework/behaviors/AttributesBehavior.php
  2. 6
      tests/framework/behaviors/AttributesBehaviorTest.php

6
framework/behaviors/AttributesBehavior.php

@ -43,7 +43,7 @@ use yii\db\ActiveRecord;
* ActiveRecord::EVENT_AFTER_VALIDATE => $fn2,
* ],
* 'attribute4' => [
* ActiveRecord::EVENT_BEFORE_DELETE => function ($event) {
* ActiveRecord::EVENT_BEFORE_DELETE => function ($event, $attribute) {
* static::disabled() || $event->isValid = false;
* },
* ],
@ -88,7 +88,7 @@ class AttributesBehavior extends Behavior
* ActiveRecord::EVENT_AFTER_VALIDATE => $fn2,
* ],
* 'attribute4' => [
* ActiveRecord::EVENT_BEFORE_DELETE => function ($event) {
* ActiveRecord::EVENT_BEFORE_DELETE => function ($event, $attribute) {
* static::disabled() || $event->isValid = false;
* },
* ],
@ -177,7 +177,7 @@ class AttributesBehavior extends Behavior
}
$value = $this->attributes[$attribute][$event->name];
if ($value instanceof Closure || (is_array($value) && is_callable($value))) {
return $value($event);
return $value($event, $attribute);
}
return $value;

6
tests/framework/behaviors/AttributesBehaviorTest.php

@ -119,7 +119,7 @@ class AttributesBehaviorTest extends TestCase
{
return [
[
'Johnny',
'name: Johnny',
[ActiveRecordWithAttributesBehavior::EVENT_BEFORE_VALIDATE => ['name', 'alias']],
// 1: name = alias; 2: alias = name; check alias
'John Doe', // name
@ -181,8 +181,8 @@ class ActiveRecordWithAttributesBehavior extends ActiveRecord
},
],
'name' => [
self::EVENT_BEFORE_VALIDATE => function ($event) {
return $event->sender->alias;
self::EVENT_BEFORE_VALIDATE => function ($event, $attribute) {
return $attribute . ': ' . $event->sender->alias;
},
],
],

Loading…
Cancel
Save