Skip to content

Commit 72d5e35

Browse files
committed
Add param onlyCoreValidators for getFileRules
1 parent 012634c commit 72d5e35

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

src/actions/UploadAction.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ class UploadAction extends Action
5656
* @var ActiveRecord $model
5757
*/
5858
private $model;
59-
/**
60-
* @see http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html
61-
* @var array $rules
62-
*/
63-
private $rules;
6459

6560
public function init()
6661
{
@@ -69,12 +64,6 @@ public function init()
6964
}
7065

7166
$this->model = new $this->modelName();
72-
$this->rules = $this->model->getFileRules($this->attribute);
73-
74-
if (isset($this->rules['imageSize'])) {
75-
$this->rules = array_merge($this->rules, $this->rules['imageSize']);
76-
unset($this->rules['imageSize']);
77-
}
7867
}
7968

8069
public function run()
@@ -87,8 +76,10 @@ public function run()
8776
);
8877
}
8978

79+
$rules = $this->model->getFileRules($this->attribute, true);
80+
9081
$model = new DynamicModel(compact('file'));
91-
$model->addRule('file', $this->type, $this->rules)->validate();
82+
$model->addRule('file', $this->type, $rules)->validate();
9283

9384
if ($model->hasErrors()) {
9485
return $this->response(['error' => $model->getFirstError('file')]);

src/behaviors/FileBehavior.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,17 @@ public function isFileProtected($attribute)
272272
* Get rules
273273
*
274274
* @param string $attribute Attribute of a model
275+
* @param bool $onlyCoreValidators Only core validators
275276
* @return array
276277
*/
277-
public function getFileRules($attribute)
278+
public function getFileRules($attribute, $onlyCoreValidators = false)
278279
{
279-
return ArrayHelper::getValue($this->attributes[$attribute], 'rules', []);
280+
$rules = ArrayHelper::getValue($this->attributes[$attribute], 'rules', []);
281+
if ($onlyCoreValidators && isset($rules['imageSize'])) {
282+
$rules = array_merge($rules, $rules['imageSize']);
283+
unset($rules['imageSize']);
284+
}
285+
return $rules;
280286
}
281287

282288
/**

tests/RulesTest.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,31 @@
1313

1414
class RulesTest extends \PHPUnit_Framework_TestCase
1515
{
16-
public function testBaseFileRules()
16+
public function testRules()
17+
{
18+
$model = new News();
19+
20+
$rules = $model->getFileRules('image_path');
21+
$this->assertArrayHasKey('imageSize', $rules);
22+
$this->assertArrayHasKey('mimeTypes', $rules);
23+
$this->assertArrayHasKey('extensions', $rules);
24+
$this->assertArrayHasKey('maxSize', $rules);
25+
$this->assertArrayHasKey('tooBig', $rules);
26+
}
27+
28+
public function testRulesWithOnlyCoreValidators()
29+
{
30+
$model = new News();
31+
32+
$rules = $model->getFileRules('image_path', true);
33+
$this->assertArrayNotHasKey('imageSize', $rules);
34+
$this->assertArrayHasKey('mimeTypes', $rules);
35+
$this->assertArrayHasKey('extensions', $rules);
36+
$this->assertArrayHasKey('maxSize', $rules);
37+
$this->assertArrayHasKey('tooBig', $rules);
38+
}
39+
40+
public function testDescription()
1741
{
1842
$model = new News();
1943

@@ -23,15 +47,15 @@ public function testBaseFileRules()
2347
$this->assertContains('File types: JPG, JPEG, PNG', $rules);
2448
}
2549

26-
public function testImageWithStrictSize()
50+
public function testDescriptionImageWithStrictSize()
2751
{
2852
$model = new News();
2953

3054
$rules = $model->getFileRulesDescription('image_strict_size');
3155
$this->assertContains('Image size: 300x300px', $rules);
3256
}
3357

34-
public function testImageWithMinAndMaxSize()
58+
public function testDescriptionImageWithMinAndMaxSize()
3559
{
3660
$model = new News();
3761

@@ -40,47 +64,47 @@ public function testImageWithMinAndMaxSize()
4064
$this->assertContains('Max. size of image: 300x300px', $rules);
4165
}
4266

43-
public function testImageWithMinSize()
67+
public function testDescriptionImageWithMinSize()
4468
{
4569
$model = new News();
4670

4771
$rules = $model->getFileRulesDescription('image_min_size');
4872
$this->assertContains('Min. size of image: 300x300px', $rules);
4973
}
5074

51-
public function testImageWithMaxSize()
75+
public function testDescriptionImageWithMaxSize()
5276
{
5377
$model = new News();
5478

5579
$rules = $model->getFileRulesDescription('image_max_size');
5680
$this->assertContains('Max. size of image: 300x300px', $rules);
5781
}
5882

59-
public function testImageWithOnlyMaxWidth()
83+
public function testDescriptionImageWithOnlyMaxWidth()
6084
{
6185
$model = new News();
6286

6387
$rules = $model->getFileRulesDescription('image_only_maxwidth');
6488
$this->assertContains('Max. width 300px', $rules);
6589
}
6690

67-
public function testImageWithOnlyMaxHeight()
91+
public function testDescriptionImageWithOnlyMaxHeight()
6892
{
6993
$model = new News();
7094

7195
$rules = $model->getFileRulesDescription('image_only_maxheight');
7296
$this->assertContains('Max. height 300px', $rules);
7397
}
7498

75-
public function testImageWithOnlyMinWidth()
99+
public function testDescriptionImageWithOnlyMinWidth()
76100
{
77101
$model = new News();
78102

79103
$rules = $model->getFileRulesDescription('image_only_minwidth');
80104
$this->assertContains('Min. width 300px', $rules);
81105
}
82106

83-
public function testImageWithOnlyMinHeight()
107+
public function testDescriptionImageWithOnlyMinHeight()
84108
{
85109
$model = new News();
86110

0 commit comments

Comments
 (0)