Skip to content

Commit

Permalink
Allow using form without property type-hints (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Oct 18, 2021
1 parent 1fac60c commit f1461a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
11 changes: 2 additions & 9 deletions src/FormModel.php
Expand Up @@ -20,7 +20,6 @@
use function explode;
use function is_subclass_of;
use function reset;
use function sprintf;
use function strpos;

/**
Expand Down Expand Up @@ -224,6 +223,7 @@ public function load(array $data, ?string $formName = null): bool
public function setAttribute(string $name, $value): void
{
[$realName] = $this->getNestedAttribute($name);

if (isset($this->attributes[$realName])) {
switch ($this->attributes[$realName]) {
case 'bool':
Expand Down Expand Up @@ -289,15 +289,8 @@ protected function collectAttributes(): array

/** @var ReflectionNamedType|null $type */
$type = $property->getType();
if ($type === null) {
throw new InvalidArgumentException(sprintf(
'You must specify the type hint for "%s" property in "%s" class.',
$property->getName(),
$property->getDeclaringClass()->getName(),
));
}

$attributes[$property->getName()] = $type->getName();
$attributes[$property->getName()] = $type !== null ? $type->getName() : '';
}

return $attributes;
Expand Down
25 changes: 15 additions & 10 deletions tests/FormModelTest.php
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Form\Tests;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Yiisoft\Form\FormModel;
use Yiisoft\Form\Tests\TestSupport\Form\FormWithNestedAttribute;
Expand Down Expand Up @@ -44,14 +43,24 @@ public function testCustomFormName(): void

public function testUnknownPropertyType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches(sprintf(
'/You must specify the type hint for "%s" property in "([^"]+)" class./',
'property',
));
$form = new class () extends FormModel {
private $property;
};

$form->setAttribute('property', true);
$this->assertSame(true, $form->getAttributeValue('property'));

$form->setAttribute('property', 'string');
$this->assertSame('string', $form->getAttributeValue('property'));

$form->setAttribute('property', 0);
$this->assertSame(0, $form->getAttributeValue('property'));

$form->setAttribute('property', 1.2563);
$this->assertSame(1.2563, $form->getAttributeValue('property'));

$form->setAttribute('property', []);
$this->assertSame([], $form->getAttributeValue('property'));
}

public function testGetAttributeValue(): void
Expand All @@ -66,10 +75,6 @@ public function testGetAttributeValue(): void

$form->rememberMe(true);
$this->assertEquals(true, $form->getAttributeValue('rememberMe'));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Undefined property: "Yiisoft\Form\Tests\TestSupport\Form\LoginForm::noExist".');
$form->getAttributeValue('noExist');
}

public function testGetAttributeValueWithNestedAttribute(): void
Expand Down

0 comments on commit f1461a5

Please sign in to comment.