-
-
Notifications
You must be signed in to change notification settings - Fork 16
LoadMultipleTrait #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
LoadMultipleTrait #214
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e643a3
Support loading multiple models with LoadMultipleTrait.php
beastbytes bb88bf1
Attempt to load all models
beastbytes 18a4d2e
Correctly extract data
beastbytes 87a31e3
Tests for LoadMultipleTrait
beastbytes d88669b
Create Time.php
beastbytes 7e63c38
Add Time.php
beastbytes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Form\Field; | ||
|
|
||
| use Yiisoft\Form\Field\Base\DateTimeInputField; | ||
|
|
||
| /** | ||
| * Represents `<input>` 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'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Form\Field; | ||
|
|
||
| use Yiisoft\Form\Field\Base\DateTimeInputField; | ||
|
|
||
| /** | ||
| * Represents `<input>` 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'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Form; | ||
|
|
||
| trait LoadMultipleTrait | ||
| { | ||
| private array $models = []; | ||
|
|
||
| public function loadMultiple(array $data, ?string $formName = null): bool | ||
| { | ||
| if ($formName === null) { | ||
| /* @var \Yiisoft\Form\FormModelInterface $this */ | ||
| $formName = $this->getFormName(); | ||
| } | ||
|
|
||
| if ($formName !== '') { | ||
| $data = $data[$formName]; | ||
| } | ||
|
|
||
| $success = true; | ||
|
|
||
| foreach (array_keys($data) as $i) { | ||
| /* @var \Yiisoft\Form\FormModelInterface $model */ | ||
| $model = clone $this; | ||
|
|
||
| if ($model->load($data[$i], '') === true) { | ||
| $this->models[] = $model; | ||
| } else { | ||
| $this->models[] = $success = false; | ||
| } | ||
| } | ||
|
|
||
| return $success; | ||
| } | ||
|
|
||
| public function getModels(): array | ||
| { | ||
| return $this->models; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Form\Tests; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Yiisoft\Form\FormModel; | ||
| use Yiisoft\Form\Tests\TestSupport\Form\LoadMultipleForm; | ||
| use Yiisoft\Form\Tests\TestSupport\TestTrait; | ||
|
|
||
| require __DIR__ . '/TestSupport/Form/NonNamespacedForm.php'; | ||
|
|
||
| final class MultipleFormModelTest extends TestCase | ||
| { | ||
| use TestTrait; | ||
|
|
||
| /* | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test need for success case. |
||
| public function testLoadMultiple(): 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', | ||
| ], | ||
| [ | ||
| '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()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Form\Tests\TestSupport\Form; | ||
|
|
||
| use Yiisoft\Form\FormModel; | ||
| use Yiisoft\Form\LoadMultipleTrait; | ||
|
|
||
| final class LoadMultipleForm extends FormModel | ||
| { | ||
| use LoadMultipleTrait; | ||
|
|
||
| private string $attribute1 = ''; | ||
| private string $attribute2 = ''; | ||
| private string $attribute3 = ''; | ||
| private string $attribute4 = ''; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use several types (false or FormModel) in array of models is confused.