Commit 101d771e by Klimov Paul

`yii\behaviors\Sluggable::validateSlug()` fixed to respect updating record

parent f27254b9
...@@ -7,10 +7,11 @@ ...@@ -7,10 +7,11 @@
namespace yii\behaviors; namespace yii\behaviors;
use yii\base\DynamicModel;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord; use yii\db\BaseActiveRecord;
use yii\helpers\Inflector; use yii\helpers\Inflector;
use yii\validators\UniqueValidator;
use Yii;
/** /**
* SluggableBehavior automatically fills the specified attribute with a value that can be used a slug in a URL. * SluggableBehavior automatically fills the specified attribute with a value that can be used a slug in a URL.
...@@ -82,15 +83,19 @@ class SluggableBehavior extends AttributeBehavior ...@@ -82,15 +83,19 @@ class SluggableBehavior extends AttributeBehavior
*/ */
public $ensureUnique = false; public $ensureUnique = false;
/** /**
* @var array configuration for slug uniqueness validator. This configuration should not contain validator name * @var array configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default
* and validated attributes - only options in format 'name => value' are allowed. * [[UniqueValidator]] will be used.
* For example: * For example:
*
* ```php
* [ * [
* 'filter' => ['type' => 1, 'status' => 2] * 'filter' => ['type' => 1, 'status' => 2]
* ] * ]
* @see yii\validators\UniqueValidator * ```
*
* @see UniqueValidator
*/ */
public $uniqueValidatorConfig = []; public $uniqueValidator = [];
/** /**
* @var callable slug unique value generator. It is used in case [[ensureUnique]] enabled and generated * @var callable slug unique value generator. It is used in case [[ensureUnique]] enabled and generated
* slug is not unique. This should be a PHP callable with following signature: * slug is not unique. This should be a PHP callable with following signature:
...@@ -179,15 +184,20 @@ class SluggableBehavior extends AttributeBehavior ...@@ -179,15 +184,20 @@ class SluggableBehavior extends AttributeBehavior
*/ */
private function validateSlug($slug) private function validateSlug($slug)
{ {
$validator = array_merge( /* @var $validator UniqueValidator */
/* @var $model BaseActiveRecord */
$validator = Yii::createObject(array_merge(
[ [
['slug'], 'class' => UniqueValidator::className()
'unique',
'targetClass' => get_class($this->owner)
], ],
$this->uniqueValidatorConfig $this->uniqueValidator
); ));
$model = DynamicModel::validateData(compact('slug'), [$validator]);
$model = clone $this->owner;
$model->clearErrors();
$model->{$this->slugAttribute} = $slug;
$validator->validateAttribute($model, $this->slugAttribute);
return !$model->hasErrors(); return !$model->hasErrors();
} }
......
...@@ -129,9 +129,15 @@ class SluggableBehaviorTest extends TestCase ...@@ -129,9 +129,15 @@ class SluggableBehaviorTest extends TestCase
$model->name = $name; $model->name = $name;
$model->save(); $model->save();
$model->save();
$this->assertEquals('test-name', $model->slug);
$model = ActiveRecordSluggableUnique::find()->one(); $model = ActiveRecordSluggableUnique::find()->one();
$model->save(); $model->save();
$this->assertEquals('test-name', $model->slug);
$model->name = 'test-name';
$model->save();
$this->assertEquals('test-name', $model->slug); $this->assertEquals('test-name', $model->slug);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment