Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
101d771e
Commit
101d771e
authored
Aug 12, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
`yii\behaviors\Sluggable::validateSlug()` fixed to respect updating record
parent
f27254b9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
12 deletions
+28
-12
SluggableBehavior.php
framework/behaviors/SluggableBehavior.php
+22
-12
SluggableBehaviorTest.php
tests/unit/framework/behaviors/SluggableBehaviorTest.php
+6
-0
No files found.
framework/behaviors/SluggableBehavior.php
View file @
101d771e
...
@@ -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 allow
ed.
*
[[UniqueValidator]] will be us
ed.
* For example:
* For example:
*
* ```php
* [
* [
* 'filter' => ['type' => 1, 'status' => 2]
* 'filter' => ['type' => 1, 'status' => 2]
* ]
* ]
* @see yii\validators\UniqueValidator
* ```
*
* @see UniqueValidator
*/
*/
public
$uniqueValidator
Config
=
[];
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
();
}
}
...
...
tests/unit/framework/behaviors/SluggableBehaviorTest.php
View file @
101d771e
...
@@ -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
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment