Framework: Exists method in validation doesn't care about soft-deletes

Created on 20 Apr 2017  路  4Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.19
  • PHP Version: 7.1
  • Database Driver & Version: PgSQL

Description:

When having this validation, it'll check if the coupon exists in the discounts table in the "code" column:

$this->validate($request, [
  'coupon'  => 'required|exists:discounts,code',
]);

That works fine, but when a code is soft-deleted already in the table, it still thinks that it exists... but it doesn't.

Steps To Reproduce:

Make an exists-validation, add a value in the database, soft delete it and run the validation.

Most helpful comment

You can use Rule::exists('discounts', 'code')->whereNull('deleted_at') to check for only undeleted items.

All 4 comments

You can use Rule::exists('discounts', 'code')->whereNull('deleted_at') to check for only undeleted items.

@themsaid why close it? it's a bug. what you offer is a workaround.

No it's not, the Validator knows nothing about Eloquent, you need to instruct it about soft deletion if you support soft deletion.

Think of the Validator as a separate component from Eloquent, it just looks into a DB table that's all it knows. Happy to help if you need any further assistance :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerbylav picture kerbylav  路  3Comments

progmars picture progmars  路  3Comments

shopblocks picture shopblocks  路  3Comments

ghost picture ghost  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments