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 Null value support for RequiredUnless Validation #525

Conversation

BadJacky
Copy link
Contributor

@BadJacky BadJacky commented Aug 5, 2023

Description

In the spatie/laravel-data package, the RequireUnless validation rule does not adequately support null values.

This PR aims to address and rectify this issue by extending the rule's functionality to incorporate null value handling.

Changes

The RequireUnless class in the package has been updated as follows:

- public function __construct(
-     string|FieldReference                           $field,
-     array|string|BackedEnum|RouteParameterReference ...$values
- ) {
+ public function __construct(
+     string|FieldReference                                $field,
+     null|array|string|BackedEnum|RouteParameterReference ...$values
+ ) {
      $this->field = $this->parseFieldReference($field);
      $this->values = Arr::flatten($values);
  }

My PHP Code

<?php

namespace App\Data;

use App\Models\User;
use Illuminate\Validation\Rule;
use Spatie\LaravelData\Attributes\Validation\Email;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Attributes\Validation\Min;
use Spatie\LaravelData\Attributes\Validation\Nullable;
use Spatie\LaravelData\Attributes\Validation\RequiredUnless;
use Spatie\LaravelData\Attributes\Validation\StringType;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Support\Validation\ValidationContext;
use Symfony\Contracts\Service\Attribute\Required;

class LoginFormData extends Data
{
    public function __construct(
        #[
            Nullable,
            RequiredUnless('email', null),
            StringType,
            Min(5),
            Max(20),
            Exists(User::class, 'username'),
        ]
        public readonly string $username,
        #[
            Nullable,
            RequiredUnless('username', null),
            StringType,
            Min(5),
            Max(20),
            Email(Email::RfcValidation),
            Exists(User::class, 'email')
        ]
        public readonly string $email,
        #[
            Required,
            StringType,
            Min(5),
            Max(20)
        ]
        public readonly string $password,
    ) {
    }

    /**
     * @return array<string, list<Rule|string>>
     */
    public static function rules(ValidationContext $context): array
    {
        return [
            'username' => ['required', 'string'],
            'password' => ['required', 'string'],
            'email'    => ['required', 'string', 'email', Rule::exists(User::class, 'email')],
        ];
    }
}

The Error Images

CleanShot 2023-08-05 at 21 42 40@2x

The screenshot after improved the RequireUnless

CleanShot 2023-08-05 at 21 43 38@2x

Impact

By extending support for null types in the RequireUnless validation rule, we gain finer control and flexibility while validating data. It also potentially resolves unforeseen issues or bugs related to the null value handling in future.

Related Documentation

require_unless in Laravel Framework

I appreciate your consideration of this PR and look forward to any feedback.

@BadJacky BadJacky marked this pull request as draft August 7, 2023 01:31
@rubenvanassche
Copy link
Member

Thanks!

@rubenvanassche rubenvanassche marked this pull request as ready for review August 9, 2023 14:07
@rubenvanassche rubenvanassche merged commit 65e5394 into spatie:main Aug 9, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants