From 0e643a3e31756650f9a9f23a77898109fc05430e Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Fri, 15 Jul 2022 12:50:25 +0100 Subject: [PATCH 1/6] Support loading multiple models with LoadMultipleTrait.php --- src/LoadMultipleTrait.php | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/LoadMultipleTrait.php diff --git a/src/LoadMultipleTrait.php b/src/LoadMultipleTrait.php new file mode 100644 index 00000000..b036345e --- /dev/null +++ b/src/LoadMultipleTrait.php @@ -0,0 +1,46 @@ +getFormName(); + } + + foreach (array_keys($data) as $i) { + /* @var \Yiisoft\Form\FormModelInterface $model */ + $model = clone $this; + + $result = false; + if ($formName === '' && !empty($data[$i])) { + $result = $model->load($data[$i], ''); + } elseif (!empty($data[$formName][$i])) { + $result = $model->load($data[$formName][$i], ''); + } + + if ($result === true) { + $this->models[] = $model; + } else { + break; + } + } + + return count($this->models) === count($data); + } + + public function getModels(): array + { + return $this->models; + } +} From bb88bf1f8aaa148690c5cc2ef187e2db022f21a3 Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Fri, 15 Jul 2022 13:13:01 +0100 Subject: [PATCH 2/6] Attempt to load all models --- src/LoadMultipleTrait.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/LoadMultipleTrait.php b/src/LoadMultipleTrait.php index b036345e..68e40d1d 100644 --- a/src/LoadMultipleTrait.php +++ b/src/LoadMultipleTrait.php @@ -6,9 +6,6 @@ trait LoadMultipleTrait { - /** - * @param FormModelInterface[] Loaded models - */ private array $models = []; public function loadMultiple(array $data, ?string $formName = null): bool @@ -18,6 +15,7 @@ public function loadMultiple(array $data, ?string $formName = null): bool $formName = $this->getFormName(); } + $success = true; foreach (array_keys($data) as $i) { /* @var \Yiisoft\Form\FormModelInterface $model */ $model = clone $this; @@ -32,11 +30,11 @@ public function loadMultiple(array $data, ?string $formName = null): bool if ($result === true) { $this->models[] = $model; } else { - break; + $this->models[] = $success = false; } } - return count($this->models) === count($data); + return $success; } public function getModels(): array From 18a4d2e3492d23dba598ae4878b570d34ac08195 Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Fri, 15 Jul 2022 16:23:17 +0100 Subject: [PATCH 3/6] Correctly extract data Simplify success/failure code --- src/LoadMultipleTrait.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/LoadMultipleTrait.php b/src/LoadMultipleTrait.php index 68e40d1d..b341bb12 100644 --- a/src/LoadMultipleTrait.php +++ b/src/LoadMultipleTrait.php @@ -15,19 +15,17 @@ public function loadMultiple(array $data, ?string $formName = null): bool $formName = $this->getFormName(); } + if ($formName !== '') { + $data = $data[$formName]; + } + $success = true; + foreach (array_keys($data) as $i) { /* @var \Yiisoft\Form\FormModelInterface $model */ $model = clone $this; - $result = false; - if ($formName === '' && !empty($data[$i])) { - $result = $model->load($data[$i], ''); - } elseif (!empty($data[$formName][$i])) { - $result = $model->load($data[$formName][$i], ''); - } - - if ($result === true) { + if ($model->load($data[$i], '') === true) { $this->models[] = $model; } else { $this->models[] = $success = false; From 87a31e3d1eb8c079660d2a3acb82c4aa11d89854 Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Fri, 15 Jul 2022 16:24:08 +0100 Subject: [PATCH 4/6] Tests for LoadMultipleTrait --- tests/MultipleFormModelTest.php | 124 ++++++++++++++++++++ tests/TestSupport/Form/LoadMultipleForm.php | 18 +++ 2 files changed, 142 insertions(+) create mode 100644 tests/MultipleFormModelTest.php create mode 100644 tests/TestSupport/Form/LoadMultipleForm.php diff --git a/tests/MultipleFormModelTest.php b/tests/MultipleFormModelTest.php new file mode 100644 index 00000000..e237a703 --- /dev/null +++ b/tests/MultipleFormModelTest.php @@ -0,0 +1,124 @@ +getFormName(); + + $data = [ + $formName => [ + [ + 'attribute1' => 'b5HhRBieacLZki2H', + 'attribute2' => 'eu3VXDpwvvyoahwNAjPmjYV7PgCEZ9pTAWAf', + 'attribute3' => 'ym8ES3nDhxN4', + 'attribute4' => 'CupHZ4Ljzeym8q8Hmu8', + ], + [ + 'attribute1' => 's8rcuBHjZ4MXEkGU2YToFGz9XTADyg4j8ibZ', + 'attribute2' => 'eDruLPmPkALRE7qUyhDapJZTTnE2A', + 'attribute3' => '2f6ot3S5f8T7tNm5', + 'attribute4' => 'rYNM4jfZ7kFCN6A4i7apSMFPxq', + ], + [ + 'attribute1' => '64brF469aKhY', + 'attribute2' => 'J3BSpg4rVY6mVq5cTu', + 'attribute3' => 'h8VpgtLbwz6WyUhCLgFgg5wkdqtmmBy5HE5U', + 'attribute4' => '6yDHpqf357fEcKsYeJz', + ], + [ + 'attribute1' => 'Mqx6LL78mysscsaHLhSSM46Uht46tdAvaejs', + 'attribute2' => '7PqV94mzGL2tP', + 'attribute3' => 'K7ZvDNkEv4cwsPpTEpBsJEu7', + 'attribute4' => 'CtsAPJQnUUCgKFGfvQ6C', + ], + [ + 'attribute1' => '49xuodLX3XPb9LueLSUq7GbhdRa3aw', + 'attribute2' => '2MHboYwmBLXyWYPJoXeoKLjVS', + 'attribute3' => 'p6AJiq3NQsW4dwcKAz', + 'attribute4' => 'JetmdpKiVXMYBs3CLvQb7qEvyQDFXELDAeCb', + ], + ], + ]; + + $this->assertTrue($form->loadMultiple($data)); + + $models = $form->getModels(); + + $this->assertCount(count($data[$formName]), $models); + + foreach ($models as $i => $model) { + $this->assertInstanceOf(LoadMultipleForm::class, $model); + $this->assertSame($data[$formName][$i], $model->getData()); + } + } + */ + + public function testLoadFailedForm(): void + { + $form = new LoadMultipleForm(); + $formName = $form->getFormName(); + + $data = [ + $formName => [ + [ + 'attribute1' => 'b5HhRBieacLZki2H', + 'attribute2' => 'eu3VXDpwvvyoahwNAjPmjYV7PgCEZ9pTAWAf', + 'attribute3' => 'ym8ES3nDhxN4', + 'attribute4' => 'CupHZ4Ljzeym8q8Hmu8', + ], + [ + 'attribute1' => 's8rcuBHjZ4MXEkGU2YToFGz9XTADyg4j8ibZ', + 'attribute2' => 'eDruLPmPkALRE7qUyhDapJZTTnE2A', + 'attribute3' => '2f6ot3S5f8T7tNm5', + 'attribute4' => 'rYNM4jfZ7kFCN6A4i7apSMFPxq', + ], + [ + 'attribute1' => '64brF469aKhY', + 'attribute2' => 'J3BSpg4rVY6mVq5cTu', + 'attribute3' => 'h8VpgtLbwz6WyUhCLgFgg5wkdqtmmBy5HE5U', + 'attribute4' => '6yDHpqf357fEcKsYeJz', + ], + [ + // empty data to cause loading failure + ], + [ + 'attribute1' => '49xuodLX3XPb9LueLSUq7GbhdRa3aw', + 'attribute2' => '2MHboYwmBLXyWYPJoXeoKLjVS', + 'attribute3' => 'p6AJiq3NQsW4dwcKAz', + 'attribute4' => 'JetmdpKiVXMYBs3CLvQb7qEvyQDFXELDAeCb', + ], + ], + ]; + + $this->assertFalse($form->loadMultiple($data)); + + $models = $form->getModels(); + + $this->assertCount(count($data[$formName]), $models); + + foreach ($models as $i => $model) { + if (empty($data[$formName][$i])) { + $this->assertFalse($model); + } else { + $this->assertInstanceOf(LoadMultipleForm::class, $model); + $this->assertSame($data[$formName][$i], $model->getData()); + } + } + } +} \ No newline at end of file diff --git a/tests/TestSupport/Form/LoadMultipleForm.php b/tests/TestSupport/Form/LoadMultipleForm.php new file mode 100644 index 00000000..1ca458b3 --- /dev/null +++ b/tests/TestSupport/Form/LoadMultipleForm.php @@ -0,0 +1,18 @@ + Date: Mon, 16 Jan 2023 16:30:59 +0000 Subject: [PATCH 5/6] Create Time.php --- src/Field/Time.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/Field/Time.php diff --git a/src/Field/Time.php b/src/Field/Time.php new file mode 100644 index 00000000..6b9165e4 --- /dev/null +++ b/src/Field/Time.php @@ -0,0 +1,22 @@ +` element of type "time" are let the user enter a time (hour, minute, second, and fraction of a + * second). + * + * @link https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time) + * @link https://developer.mozilla.org/docs/Web/HTML/Element/input/time + */ +final class Time extends DateTimeInputField +{ + protected function getInputType(): string + { + return 'time'; + } +} From 7e63c38695e739fd28af71977988f4196373c51a Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Mon, 16 Jan 2023 16:42:16 +0000 Subject: [PATCH 6/6] Add Time.php --- Time.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Time.php diff --git a/Time.php b/Time.php new file mode 100644 index 00000000..6b9165e4 --- /dev/null +++ b/Time.php @@ -0,0 +1,22 @@ +` element of type "time" are let the user enter a time (hour, minute, second, and fraction of a + * second). + * + * @link https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time) + * @link https://developer.mozilla.org/docs/Web/HTML/Element/input/time + */ +final class Time extends DateTimeInputField +{ + protected function getInputType(): string + { + return 'time'; + } +}