Nullable nested data fails validation when nested data attributes are required #55
-
Not sure if this behavior is expected, but here we go: I have two data objects (simplified below)
and
The idea is that both $mainActivity and $otherActivities in the first Data object can be nullable. However, when Here are the rules I see the package is generating for these attributes (debug from within
The same doesn't happen for the Am I missing any configuration on the |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 11 replies
-
Hi @ominixamf, This was indeed a bug, it should be fixed with version 1.3.0! |
Beta Was this translation helpful? Give feedback.
-
Hey guys First of all, this lib is awesome! Really! I just came across an issue which confuses me a bit, as it was already solved and yet I have the same error with v2.0.12. class UpdateUser extends Data
{
public function __construct(
public ?SetUserProfile $setUserProfile,
public ?SetUserRoles $setUserRoles
) {
}
}
class SetUserProfile extends Data
{
public function __construct(
#[Required] public string $name
) {
}
}
Running this through a request with the following payload: $payload = [
'setUserRoles' => ['some', roles]
] Results in a validation-error: The set user profile.name field is required. Is this the intended behaviour? If not, how can I stop the validation on nested Data-objects if none has been provided? Cheers! |
Beta Was this translation helpful? Give feedback.
-
Just if anybody is interested, the class RequiredWithThisRuleInferrer implements RuleInferrer
{
public function handle(
DataProperty $property,
RulesCollection $rules
): RulesCollection {
$property->attributes
->filter(fn(object $attribute) => $attribute instanceof RequiredWithThis)
->each(fn(RequiredWithThis $rule) => $rule->setClass($property->className));
return $rules;
}
} The Attribute which will be used for validation: #[Attribute(Attribute::TARGET_PROPERTY)]
class RequiredWithThis extends StringValidationAttribute implements RequiringRule
{
protected string $class;
public static function keyword(): string
{
return 'required_with';
}
public function setClass(string $class)
{
$name = collect(explode('\\', $class))->last();
$this->class = Str::camel($name);
}
public function parameters(): array
{
return [$this->normalizeValue($this->class)];
}
} And finally, how I use it: class SetUserProfile extends Data
{
public function __construct(
#[RequiredWithThis] public string $firstname,
#[RequiredWithThis] public string $lastname
) {
}
} Rendererd rule: "setUserProfile.firstname" => array:3 [
0 => "nullable"
1 => "string"
2 => "required_with:setUserProfile"
] Again, maybe not the best way. But at least the solution is not interfering with the core of the package. Any better suggestions are welcome! |
Beta Was this translation helpful? Give feedback.
-
The problem is kinda similar to the issue @francoism90 made, though not using 'sometimes' but 'nullable'. I've asked the Laravel developers what the best solution would be: laravel/framework#44586 |
Beta Was this translation helpful? Give feedback.
-
We've got progress! Hallelujah! Active github issue: |
Beta Was this translation helpful? Give feedback.
Hi @ominixamf,
This was indeed a bug, it should be fixed with version 1.3.0!