Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[11.x] Add RequiredIfDeclined validation rule #51030

Merged
merged 1 commit into from
Apr 12, 2024

Conversation

timmydhooghe
Copy link
Contributor

@timmydhooghe timmydhooghe commented Apr 11, 2024

Description

This PR adds a required_if_declined rule. The field under validation must be present and not empty if the other field field is declined (it uses the existing validateDeclined rule).

☑️ A test is added.
I'll create a PR for the documentation if this gets merged

Use case

Entering an expense:

  • if the cost is recurring (subscription), we need to know the frequency (weekly, monthy, yearly)
    • required_if_accepted can be used to make the frequency mandatory
  • if the cost is one-off (restaurant receipt), we need the receipt date.
    • required_if_declined can be used to make the receipt date mandatory

Recurring:

image

One-off:

image
Validator::make([
    'is_recurring' => '0',
    'receipt_date' => '2024-04-11',
], [
    'is_recurring' => 'required|boolean',
    'receipt_date' => 'required_if_declined:is_recurring',
]);

@timmydhooghe
Copy link
Contributor Author

@taylorotwell There's one test failing:

1) Illuminate\Tests\Encryption\EncrypterTest::testRawStringEncryptionWithPreviousKeys

Failed asserting that two strings are identical.

I only added a test in tests/Validation/ValidationValidatorTest.php, so I'm a bit confused.

@taylorotwell taylorotwell merged commit 393ccc6 into laravel:11.x Apr 12, 2024
25 of 30 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants