Skip to content

MessageBag emtpy when using "Unique" rule on transaction #229

@giulioprovasi

Description

@giulioprovasi

Hey,
after digging a while on a weird issue, here what I get :

Model rule:

'name' => [
                'bail',
                'required',
                'string',
                'max:255',
                (new Rules\Unique('folders', 'name'))
                    ->ignore($this->id)
                    ->where('parent_id', $this->parent_id)
                    ->whereNull($this->getQualifiedDiscardedAtColumn())
                    ->whereNull($this->getQualifiedDeletedAtColumn())
                ,
            ],
 DB::transaction(static function() {
            $x = new Test();
            $x->name = 'test'; # name is not unique, so it will trigger Unicity rule
            $x->saveOrFail(); # fails correctly
        });

The issue is that in the following method

    /**
     * Throw a validation exception.
     *
     * @throws \Watson\Validating\ValidationException
     */
    public function throwValidationException()
    {
        $validator = $this->makeValidator($this->getRules());

        throw new ValidationException($validator, $this);
    }

The validator is not "triggered" with a ->fail(), so message bag is empty right now.
Then, my transaction rollback and the unique rule does not fails anymore, so the validator has no messages :/

Won't it be better to do something like this :


    public function throwValidationException()
    {
        $validator = $this->makeValidator($this->getRules());

        throw new ValidationException(tap($validator)->fails(), $this);
    }

in order to populate the validator with errors before anything else in the app happens ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions