Home > Software design >  Laravel validation sometimes and same rules
Laravel validation sometimes and same rules

Time:07-19

I'm trying to validate a form by checking if 2 fields are equal only if they are both provided (only one field or even none can be provided). I tried building my validation rule this way but it always return that my 2 fields must match even if one is not provided.

$validator = Validator::make($inputFields, [
            ...
            'brandIdOnline' => 'uuid|sometimes|same:brandIdOffline',
            'brandIdOffline' => 'uuid|sometimes|same:brandIdOnline',
            ...
        ]);

I guess the sometimes rule is not working here since it's applying to the field provided and not checking the other filed (the one I want to make the match with). Is it possible to achieve that simply or should I build my own rule?

Thank you.

CodePudding user response:

I think sometimes is not needed here, but what can help you is exclude-without rule

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class ValidatorTest extends TestCase
{
    use WithFaker;

    private array $validationRules = [
        'brandIdOnline' => 'uuid|exclude_without:brandIdOffline|same:brandIdOffline',
        'brandIdOffline' => 'uuid|exclude_without:brandIdOnline|same:brandIdOnline',
    ];

    public function testValidatorShouldFailIfBothAreDifferent()
    {
        $inputFields = [
            'brandIdOnline' => $this->faker->uuid(),
            'brandIdOffline' => $this->faker->uuid(),
        ];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertFalse($validator->passes());
    }

    public function testValidatorShouldFailIfBothAreSameButNotUUID()
    {
        $color = $this->faker->colorName();
        $inputFields = [
            'brandIdOnline' => $color,
            'brandIdOffline' => $color,
        ];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertFalse($validator->passes());
    }

    public function testValidatorShouldPassIfBothAreSame()
    {
        $uuid = $this->faker->uuid();
        $inputFields = [
            'brandIdOnline' => $uuid,
            'brandIdOffline' => $uuid,
        ];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertTrue($validator->passes());
    }

    public function testValidatorShouldPassIfBothAreMissing()
    {
        $inputFields = [];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertTrue($validator->passes());

    }

    public function testValidatorShouldPassIfBothAreEmpty()
    {
        $inputFields = [
            'brandIdOnline' => '',
            'brandIdOffline' => '',
        ];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertTrue($validator->passes());
    }

    public function testValidatorShouldPassIfOnlyBrandIdOnlineProvided()
    {
        $inputFields = [
            'brandIdOnline' => $this->faker->uuid(),
        ];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertTrue($validator->passes());
    }

    public function testValidatorShouldPassIfOnlyBrandIdOfflineProvided()
    {
        $inputFields = [
            'brandIdOffline' => $this->faker->uuid(),
        ];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertTrue($validator->passes());
    }

    public function testValidatorShouldFailIfOnlyBrandIdOnlineProvidedButNotUUID()
    {
        $inputFields = [
            'brandIdOnline' => $this->faker->colorName(),
        ];

        $validator = Validator::make($inputFields, $this->validationRules);

        self::assertFalse($validator->passes());
    }
}
  • Related