Skip to content
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

Add #[Required] attribute #9084

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/Attributes/Required.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Livewire\Attributes;

use Attribute;
use Livewire\Features\SupportRequiredProperties\BaseRequired;

#[Attribute(Attribute::TARGET_PROPERTY)]
class Required extends BaseRequired
{
//
}
12 changes: 12 additions & 0 deletions src/Features/SupportRequiredProperties/BaseRequired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Livewire\Features\SupportRequiredProperties;

use Attribute;
use Livewire\Features\SupportAttributes\Attribute as LivewireAttribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class BaseRequired extends LivewireAttribute
{
//
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Livewire\Features\SupportRequiredProperties;

use Illuminate\Support\Str;

class RequiredPropertyNotProvidedException extends \Exception
{
public function __construct($componentName, $missingProperties)
{
parent::__construct(sprintf(
'Missing required %s [%s] in component [%s]',
Str::plural('property', $missingProperties->count()),
$missingProperties->implode(', '),
$componentName,
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Livewire\Features\SupportRequiredProperties;

use Livewire\ComponentHook;

class SupportRequiredProperties extends ComponentHook
{
public function mount($params)
{
$requiredProperties = $this->component
->getAttributes()
->whereInstanceOf(BaseRequired::class)
->map(fn ($property) => $property->getName());

$missingProperties = $requiredProperties->diff(array_keys($params));

throw_if(
$missingProperties->count(),
new RequiredPropertyNotProvidedException($this->component->getName(), $missingProperties),
);
}
}
64 changes: 64 additions & 0 deletions src/Features/SupportRequiredProperties/UnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Features\SupportRequiredProperties;

use Livewire\Attributes\Required;
use Livewire\Livewire;
use Tests\TestCase;
use Tests\TestComponent;

class UnitTest extends TestCase
{
public function test_unprovided_required_property_triggers_exception()
{
$this->expectExceptionMessageMatches(
'/Missing required property \[foo] in component \[.*]/',
);

Livewire::test(new class extends TestComponent {
#[Required]
public string $foo;
});
}

public function test_multiple_unprovided_required_properties_triggers_exception()
{
$this->expectExceptionMessageMatches(
'/Missing required properties \[foo, bar] in component \[.*]/',
);

Livewire::test(new class extends TestComponent {
#[Required]
public string $foo;

#[Required]
public string $bar;
});
}

public function test_providing_required_properties_renders_component()
{
Livewire::test(new class extends TestComponent {
#[Required]
public string $foo;

#[Required]
public string $bar;
}, ['foo' => '1', 'bar' => '2'])
->assertOk()
->assertSetStrict('foo', '1')
->assertSetStrict('bar', '2');
}

public function test_provided_required_properties_with_incorrect_types_are_still_applied()
{
$this->expectExceptionMessage(
'Cannot assign null to property Tests\TestComponent@anonymous::$foo of type string',
);

Livewire::test(new class extends TestComponent {
#[Required]
public string $foo;
}, ['foo' => null]);
}
}
1 change: 1 addition & 0 deletions src/LivewireServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ protected function bootFeatures()
Features\SupportNestedComponentListeners\SupportNestedComponentListeners::class,
Features\SupportMorphAwareIfStatement\SupportMorphAwareIfStatement::class,
Features\SupportAutoInjectedAssets\SupportAutoInjectedAssets::class,
Features\SupportRequiredProperties\SupportRequiredProperties::class,
Features\SupportComputed\SupportLegacyComputedPropertySyntax::class,
Features\SupportNestingComponents\SupportNestingComponents::class,
Features\SupportScriptsAndAssets\SupportScriptsAndAssets::class,
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy