Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Time.php
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';
}
}
22 changes: 22 additions & 0 deletions src/Field/Time.php
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';
}
}
42 changes: 42 additions & 0 deletions src/LoadMultipleTrait.php
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;
Copy link
Copy Markdown
Member

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.

}
}

return $success;
}

public function getModels(): array
{
return $this->models;
}
}
124 changes: 124 additions & 0 deletions tests/MultipleFormModelTest.php
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;

/*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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());
}
}
}
}
18 changes: 18 additions & 0 deletions tests/TestSupport/Form/LoadMultipleForm.php
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 = '';
}