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

exclude, exclude_with rules not working #432

Closed
emilebourquin opened this issue May 2, 2023 · 2 comments
Closed

exclude, exclude_with rules not working #432

emilebourquin opened this issue May 2, 2023 · 2 comments

Comments

@emilebourquin
Copy link

emilebourquin commented May 2, 2023

Note: this is an oversimplification to show the issues I have been experiencing with exclude , exclude_with, and other exclude... rules in Data objects.

Given the form:

<form method="POST" action="/emailAddresses">
    @csrf
    Email: <input type="email" name="email" value="test1@example.com" /><br />
    MD5: <input type="text" name="email_md5" value="1234567890abcdef1234567890abcdef" /><br />
    <input type="submit" name="submit" value="Submit" />
</form>

the route:

Route::post('/emailAddresses', [EmailAddressController::class, 'store']);

the EmailAddressController store function:

public function store(EmailAddressData $emailAddressData)
{
    $theData = $emailAddressData->toArray();
    dd($theData);
}

the EmailAddressData DTO:

<?php
namespace App\Data;
use Spatie\LaravelData\Data;

class EmailAddressData extends Data
{
    public function __construct(
        public readonly ?string $email,
        public readonly ?string $email_md5,
    ) {}

    public static function rules()
    {
        return [
            'email' => ['email'],
            'email_md5' => ['exclude_with:email'],
        ];
    }
}

dd($theData); shows:

array:2 [▼ // app/Http/Controllers/EmailAddressController.php:42
  "email" => "test1@example.com"
  "email_md5" => "1234567890abcdef1234567890abcdef"
]

Note that email_md5 is not excluded. Even using the email_md5 rule as exclude fails to exclude the email_md5 from the validated data.

However, using the exact same rules in a FormRequest does exclude the md5_email from the validated data:

public function store(StoreEmailAddressRequest $request)
{
    $theData = $request->validated();
    dd($theData);
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;

class StoreEmailAddressRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules()
    {
        return [
            'email' => ['email'],
            'email_md5' => ['exclude_with:email'],
        ];
    }
}

dd($theData) shows:

array:1 [▼ // app/Http/Controllers/EmailAddressController.php:42
  "email" => "test1@example.com"
]
@rubenvanassche
Copy link
Member

This should be fixed with the next version!

@rubenvanassche
Copy link
Member

We're going to revert this change, we have 2 issues currently:

At the moment validation works with both the mapped and non mapped names, but the mapped version name is always taken when present. We'll need to make some changes here to always use the data object property name. We'll do this in v4 because it's breaking stuff. At the moment the validator returns the mapped name but we need the non mapped version at this point to continue.

Also something we need to take a look at, v4 will be the best place for this.

This is the commit, it can easily be added locally for now: afe0ac7

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

No branches or pull requests

2 participants