Closed RasmusGodske closed 3 weeks ago
Technically I would not initialize an optional property, the package will do this for you (I'm even thinking of making the optional constructor private in v5). But it could be a beginner mistake so the issue should be fixed.
I get that, however the problem with that solution, is the fact the intelisense would act out, because according to it, the field is required. You would have to provide it either a Optional
object or an int
. By initializing it to Optional
, the intellisense also see it as optional.
class UpdateProductData extends Data
{
public function __construct(
#[Required]
#[WithCast(EnumCast::class)]
public SomeEnumType $type,
#[RequiredIf('type', [SomeEnumType::FOO])]
public Optional|string $bar = new Optional,
) {}
}
// InteliSense will accept this
$updateProductData = new UpdateProductData(
SomeEnumType::FOO
)
class UpdateProductData extends Data
{
public function __construct(
#[Required]
#[WithCast(EnumCast::class)]
public SomeEnumType $type,
#[RequiredIf('type', [SomeEnumType::FOO])]
public Optional|string $bar;
) {}
}
// InteliSense will show error
// Expected 2 arguments. Found 1.intelephense(P1005)
$updateProductData = new UpdateProductData(
SomeEnumType::FOO
)
✏️ Describe the bug Optional properties are not properly handled when using the
RequiredIf
rule, specifically when the optional property is initialized with new Optional. When theRequiredIf
rule is applied AND the condition is met, the validation does not enforce the "required" status for properties initialized withnew Optional
, while it does work correctly for properties declared asOptional|string
without initialization.↪️ To Reproduce Provide us a pest test like this one which shows the problem:
In this example, the you would expect both attributes:
$bar
and$foobar
to be required, since$type == 'foo'
. However only$foobar
have arequired
rule.This bug might extend to other validation rules such as
RequireWith
, however I have not tested that part.✅ Expected behavior It would be expected that both attributes
$bar
and$foobar
, would be required.🖥️ Versions
Laravel: 10.0 Laravel Data: 4.10.0 PHP: 8.3.9