From eaf516ba1796d2276646e3adef45d22b3c4cd217 Mon Sep 17 00:00:00 2001 From: BlakeXu <9218664+Blake-xuhyy@user.noreply.gitee.com> Date: Thu, 24 Jun 2021 11:07:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DappendCheckField=E4=B8=8D?= =?UTF-8?q?=E4=BC=9A=E5=A2=9E=E5=8A=A0=E5=8E=9F=E8=A7=84=E5=88=99=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Support/RuleManagerScene.php | 16 ++++++---- src/Support/ValidateScene.php | 6 ++-- tests/Test/TestBug.php | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/Support/RuleManagerScene.php b/src/Support/RuleManagerScene.php index fb05760..a15a6f0 100644 --- a/src/Support/RuleManagerScene.php +++ b/src/Support/RuleManagerScene.php @@ -22,19 +22,25 @@ class RuleManagerScene implements SceneInterface */ protected $checkRules; + /** + * All original validation rules + * @var array + */ + protected $rules = []; + /** * RuleManagerScene constructor. - * @param array $checkRules The rules to be applied to the data. + * @param array $rules All original validation rules */ - public function __construct(array $checkRules = []) + public function __construct(array $rules = []) { - $this->checkRules = $checkRules; + $this->rules = $rules; } /** @inheritDoc */ public function only(array $fields): SceneInterface { - $this->checkRules = array_intersect_key($this->checkRules, array_flip($fields)); + $this->checkRules = array_intersect_key($this->rules, array_flip($fields)); return $this; } @@ -103,7 +109,7 @@ class RuleManagerScene implements SceneInterface /** @inheritDoc */ public function appendCheckField(string $field): SceneInterface { - $rule = $this->checkRules[$field] ?? ''; + $rule = $this->rules[$field] ?? ''; $this->checkRules = array_merge($this->checkRules, [$field => $rule]); return $this; } diff --git a/src/Support/ValidateScene.php b/src/Support/ValidateScene.php index 669d20b..1910929 100644 --- a/src/Support/ValidateScene.php +++ b/src/Support/ValidateScene.php @@ -77,12 +77,12 @@ class ValidateScene extends RuleManagerScene /** * ValidateScene constructor. - * @param array $checkRules + * @param array $rules * @param array $checkData */ - public function __construct(array $checkRules = [], array $checkData = []) + public function __construct(array $rules = [], array $checkData = []) { - parent::__construct($checkRules); + parent::__construct($rules); $this->checkData = $checkData; } diff --git a/tests/Test/TestBug.php b/tests/Test/TestBug.php index 710b530..127ceb1 100644 --- a/tests/Test/TestBug.php +++ b/tests/Test/TestBug.php @@ -13,6 +13,7 @@ namespace W7\Tests\Test; use W7\Tests\Material\BaseTestValidate; +use W7\Validate\Exception\ValidateException; use W7\Validate\Support\ValidateScene; use W7\Validate\Validate; @@ -41,4 +42,53 @@ class TestBug extends BaseTestValidate $this->assertArrayHasKey('name', $v->checkData); $this->assertEquals(123, $v->checkData['name']); } + + public function testBug6() + { + $v = new class extends Validate { + protected $rule = [ + 'a' => 'required', + 'b' => 'required', + 'c' => 'required', + ]; + + protected $message = [ + 'a.required' => 'a不能为空', + 'b.required' => 'b不能为空', + 'c.required' => 'c不能为空', + ]; + + protected function sceneTest(ValidateScene $scene) + { + $scene->only(['a']) + ->appendCheckField('b') + ->appendCheckField('c'); + } + }; + + try { + $v->scene('test')->check([ + 'a' => 1 + ]); + } catch (ValidateException $e) { + $this->assertEquals('b不能为空', $e->getMessage()); + } + + try { + $v->scene('test')->check([ + 'a' => 1, + 'b' => 1 + ]); + } catch (ValidateException $e) { + $this->assertEquals('c不能为空', $e->getMessage()); + } + + $data = $v->scene('test')->check([ + 'a' => 1, + 'b' => 1, + 'c' => 1, + ]); + + $this->assertEmpty(array_diff_key($data, array_flip(['a', 'b', 'c']))); + } } -- Gitee