-
Notifications
You must be signed in to change notification settings - Fork 638
/
ElementUriValidator.php
64 lines (55 loc) · 1.6 KB
/
ElementUriValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\validators;
use Craft;
use craft\base\Element;
use craft\errors\OperationAbortedException;
use craft\helpers\ElementHelper;
use yii\base\InvalidConfigException;
/**
* Class ElementUriValidator.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class ElementUriValidator extends UriValidator
{
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->skipOnEmpty = false;
}
/**
* @inheritdoc
* @throws InvalidConfigException if $attribute is not 'uri'
*/
public function validateAttribute($model, $attribute)
{
if ($attribute !== 'uri') {
throw new InvalidConfigException('Invalid use of ElementUriValidator');
}
// If this is a draft or revision and it already has a URI, leave it alone
/** @var Element $model */
if (($model->getIsDraft() || $model->getIsRevision()) && $model->uri) {
return;
}
try {
ElementHelper::setUniqueUri($model);
} catch (OperationAbortedException $e) {
$this->addError($model, $attribute, Craft::t('app', 'Could not generate a unique URI based on the URI format.'));
return;
}
if (!$this->isEmpty($model->uri)) {
parent::validateAttribute($model, $attribute);
}
}
}